@@ -28,8 +28,8 @@ struct RenderTile { | |||
}; | |||
struct RenderCamera { | |||
SwanCommon::Vec2 pos; | |||
SwanCommon::Vec2i size; | |||
SwanCommon::Vec2 pos = {0, 0}; | |||
SwanCommon::Vec2i size = {1, 1}; | |||
float zoom = 1; | |||
}; | |||
@@ -57,6 +57,8 @@ public: | |||
RenderSprite createSprite(void *data, int width, int height); | |||
void destroySprite(RenderSprite sprite); | |||
SwanCommon::Vec2 winScale() { return winScale_; } | |||
private: | |||
struct DrawChunk { | |||
SwanCommon::Vec2 pos; | |||
@@ -69,6 +71,8 @@ private: | |||
RenderSprite sprite; | |||
}; | |||
SwanCommon::Vec2 winScale_ = {1, 1}; | |||
std::unique_ptr<RendererState> state_; | |||
std::vector<DrawChunk> drawChunks_; |
@@ -149,11 +149,17 @@ Renderer::~Renderer() = default; | |||
void Renderer::draw(const RenderCamera &cam) { | |||
Mat3gf camMat; | |||
// Make the matrix translate to { -camX, -camY }, fix up the aspect ratio, | |||
// flip the Y axis so that positive Y direction is down, and scale according to zoom. | |||
float ratio = (float)cam.size.y / (float)cam.size.x; | |||
camMat.translate(-cam.pos); | |||
camMat.scale({cam.zoom * ratio, -cam.zoom}); | |||
if (cam.size.y > cam.size.x) { | |||
float ratio = (float)cam.size.y / (float)cam.size.x; | |||
winScale_ = {1/ratio, 1}; | |||
camMat.scale({cam.zoom * ratio, -cam.zoom}); | |||
} else { | |||
float ratio = (float)cam.size.x / (float)cam.size.y; | |||
winScale_ = {1, 1/ratio}; | |||
camMat.scale({cam.zoom, -cam.zoom * ratio}); | |||
} | |||
auto &chunkProg = state_->chunkProg; | |||
auto &spriteProg = state_->spriteProg; |
@@ -28,17 +28,17 @@ public: | |||
} | |||
void onMouseMove(Sint32 x, Sint32 y) { | |||
mousePos_ = {x, y}; | |||
mousePos_ = (Vec2{(float)x, (float)y} / (Vec2)cam_.size) * renderer_.winScale(); | |||
} | |||
void onMouseDown(Sint32 x, Sint32 y, Uint8 button) { | |||
mousePos_ = {x, y}; | |||
onMouseMove(x, y); | |||
pressedButtons_[button] = true; | |||
didPressButtons_[button] = true; | |||
} | |||
void onMouseUp(Sint32 x, Sint32 y, Uint8 button) { | |||
mousePos_ = {x, y}; | |||
onMouseMove(x, y); | |||
pressedButtons_[button] = false; | |||
didReleaseButtons_[button] = true; | |||
} | |||
@@ -50,7 +50,7 @@ public: | |||
bool isKeyPressed(SDL_Scancode code) { return pressedKeys_[code]; } | |||
bool wasKeyPressed(SDL_Scancode code) { return didPressKeys_[code]; } | |||
bool wasKeyReleased(SDL_Scancode code) { return didReleaseKeys_[code]; } | |||
Vec2i getMousePos() { return mousePos_; } | |||
Vec2 getMousePos() { return mousePos_; } | |||
bool isMousePressed(Uint8 button) { return pressedButtons_[button]; } | |||
bool wasMousePressed(Uint8 button) { return didPressButtons_[button]; } | |||
bool wasMouseReleased(Uint8 button) { return didReleaseButtons_[button]; } | |||
@@ -65,14 +65,14 @@ public: | |||
std::unique_ptr<World> world_ = NULL; | |||
Cygnet::Renderer renderer_; | |||
Cygnet::RenderCamera cam_{ .zoom = 0.25 }; | |||
Cygnet::RenderCamera cam_{.zoom = 0.25}; | |||
private: | |||
std::bitset<SDL_NUM_SCANCODES> pressedKeys_; | |||
std::bitset<SDL_NUM_SCANCODES> didPressKeys_; | |||
std::bitset<SDL_NUM_SCANCODES> didReleaseKeys_; | |||
Vec2i mousePos_; | |||
Vec2 mousePos_; | |||
std::bitset<SDL_BUTTON_X2> pressedButtons_; | |||
std::bitset<SDL_BUTTON_X2> didPressButtons_; | |||
std::bitset<SDL_BUTTON_X2> didReleaseButtons_; |
@@ -19,10 +19,8 @@ void Game::createWorld(const std::string &worldgen, const std::vector<std::strin | |||
} | |||
TilePos Game::getMouseTile() { | |||
auto mousePos = getMousePos(); | |||
return TilePos( | |||
(int)floor(cam_.pos.x + mousePos.x / (Swan::TILE_SIZE * cam_.zoom)), | |||
(int)floor(cam_.pos.y + mousePos.y / (Swan::TILE_SIZE * cam_.zoom))); | |||
auto pos = (getMousePos() * 2 - renderer_.winScale()) / cam_.zoom + cam_.pos; | |||
return TilePos{(int)floor(pos.x), (int)floor(pos.y)}; | |||
} | |||
SDL_Color Game::backgroundColor() { |