Browse Source

compute tile pos

feature/replace-renderer
Martin Dørum 3 years ago
parent
commit
164d7bde06

+ 6
- 2
libcygnet/include/cygnet/Renderer.h View File

}; };


struct RenderCamera { struct RenderCamera {
SwanCommon::Vec2 pos;
SwanCommon::Vec2i size;
SwanCommon::Vec2 pos = {0, 0};
SwanCommon::Vec2i size = {1, 1};
float zoom = 1; float zoom = 1;
}; };


RenderSprite createSprite(void *data, int width, int height); RenderSprite createSprite(void *data, int width, int height);
void destroySprite(RenderSprite sprite); void destroySprite(RenderSprite sprite);


SwanCommon::Vec2 winScale() { return winScale_; }

private: private:
struct DrawChunk { struct DrawChunk {
SwanCommon::Vec2 pos; SwanCommon::Vec2 pos;
RenderSprite sprite; RenderSprite sprite;
}; };


SwanCommon::Vec2 winScale_ = {1, 1};

std::unique_ptr<RendererState> state_; std::unique_ptr<RendererState> state_;


std::vector<DrawChunk> drawChunks_; std::vector<DrawChunk> drawChunks_;

+ 10
- 4
libcygnet/src/Renderer.cc View File

void Renderer::draw(const RenderCamera &cam) { void Renderer::draw(const RenderCamera &cam) {
Mat3gf camMat; 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.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 &chunkProg = state_->chunkProg;
auto &spriteProg = state_->spriteProg; auto &spriteProg = state_->spriteProg;

+ 6
- 6
libswan/include/swan/Game.h View File

} }


void onMouseMove(Sint32 x, Sint32 y) { 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) { void onMouseDown(Sint32 x, Sint32 y, Uint8 button) {
mousePos_ = {x, y};
onMouseMove(x, y);
pressedButtons_[button] = true; pressedButtons_[button] = true;
didPressButtons_[button] = true; didPressButtons_[button] = true;
} }


void onMouseUp(Sint32 x, Sint32 y, Uint8 button) { void onMouseUp(Sint32 x, Sint32 y, Uint8 button) {
mousePos_ = {x, y};
onMouseMove(x, y);
pressedButtons_[button] = false; pressedButtons_[button] = false;
didReleaseButtons_[button] = true; didReleaseButtons_[button] = true;
} }
bool isKeyPressed(SDL_Scancode code) { return pressedKeys_[code]; } bool isKeyPressed(SDL_Scancode code) { return pressedKeys_[code]; }
bool wasKeyPressed(SDL_Scancode code) { return didPressKeys_[code]; } bool wasKeyPressed(SDL_Scancode code) { return didPressKeys_[code]; }
bool wasKeyReleased(SDL_Scancode code) { return didReleaseKeys_[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 isMousePressed(Uint8 button) { return pressedButtons_[button]; }
bool wasMousePressed(Uint8 button) { return didPressButtons_[button]; } bool wasMousePressed(Uint8 button) { return didPressButtons_[button]; }
bool wasMouseReleased(Uint8 button) { return didReleaseButtons_[button]; } bool wasMouseReleased(Uint8 button) { return didReleaseButtons_[button]; }


std::unique_ptr<World> world_ = NULL; std::unique_ptr<World> world_ = NULL;
Cygnet::Renderer renderer_; Cygnet::Renderer renderer_;
Cygnet::RenderCamera cam_{ .zoom = 0.25 };
Cygnet::RenderCamera cam_{.zoom = 0.25};


private: private:
std::bitset<SDL_NUM_SCANCODES> pressedKeys_; std::bitset<SDL_NUM_SCANCODES> pressedKeys_;
std::bitset<SDL_NUM_SCANCODES> didPressKeys_; std::bitset<SDL_NUM_SCANCODES> didPressKeys_;
std::bitset<SDL_NUM_SCANCODES> didReleaseKeys_; std::bitset<SDL_NUM_SCANCODES> didReleaseKeys_;


Vec2i mousePos_;
Vec2 mousePos_;
std::bitset<SDL_BUTTON_X2> pressedButtons_; std::bitset<SDL_BUTTON_X2> pressedButtons_;
std::bitset<SDL_BUTTON_X2> didPressButtons_; std::bitset<SDL_BUTTON_X2> didPressButtons_;
std::bitset<SDL_BUTTON_X2> didReleaseButtons_; std::bitset<SDL_BUTTON_X2> didReleaseButtons_;

+ 2
- 4
libswan/src/Game.cc View File

} }


TilePos Game::getMouseTile() { 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() { SDL_Color Game::backgroundColor() {

Loading…
Cancel
Save