Implement sprite movement
This commit is contained in:
35
main.cpp
35
main.cpp
@@ -23,6 +23,8 @@ struct Position {
|
|||||||
int main() {
|
int main() {
|
||||||
spdlog::info("Initialize SDL...");
|
spdlog::info("Initialize SDL...");
|
||||||
|
|
||||||
|
SDL_SetHint(SDL_HINT_RENDER_VSYNC, "1");
|
||||||
|
|
||||||
if (!SDL_Init(SDL_INIT_VIDEO)) {
|
if (!SDL_Init(SDL_INIT_VIDEO)) {
|
||||||
spdlog::critical("Failed to initialize SDL!\nCause: {}", SDL_GetError());
|
spdlog::critical("Failed to initialize SDL!\nCause: {}", SDL_GetError());
|
||||||
std::terminate();
|
std::terminate();
|
||||||
@@ -53,10 +55,13 @@ int main() {
|
|||||||
.texture_atlas_index = 0,
|
.texture_atlas_index = 0,
|
||||||
.custom_size = Vec2{.x = WINDOW_WIDTH, .y = WINDOW_HEIGHT}});
|
.custom_size = Vec2{.x = WINDOW_WIDTH, .y = WINDOW_HEIGHT}});
|
||||||
|
|
||||||
|
struct Fruit {};
|
||||||
|
|
||||||
world.entity("Sprite1")
|
world.entity("Sprite1")
|
||||||
.set<Position>(Position{.x = 0, .y = 0})
|
.set<Position>(Position{.x = 0, .y = 0})
|
||||||
.set<Sprite>(
|
.set<Sprite>(
|
||||||
Sprite{.texture = &texture_assets->fruits, .texture_atlas_index = 0});
|
Sprite{.texture = &texture_assets->fruits, .texture_atlas_index = 0})
|
||||||
|
.add<Fruit>();
|
||||||
|
|
||||||
world.entity("Sprite2")
|
world.entity("Sprite2")
|
||||||
.set<Position>(Position{.x = 32, .y = 32})
|
.set<Position>(Position{.x = 32, .y = 32})
|
||||||
@@ -66,8 +71,8 @@ int main() {
|
|||||||
world.system<SdlHandles const, Position const, Sprite const>("RenderSprites")
|
world.system<SdlHandles const, Position const, Sprite const>("RenderSprites")
|
||||||
.term_at(0)
|
.term_at(0)
|
||||||
.singleton()
|
.singleton()
|
||||||
.each([](flecs::entity e, SdlHandles const &sdl_handles,
|
.each([](SdlHandles const &sdl_handles, Position const &pos,
|
||||||
Position const &pos, Sprite const &sprite) {
|
Sprite const &sprite) {
|
||||||
TextureAtlasLayout layout = sprite.texture->texture_atlas_layout;
|
TextureAtlasLayout layout = sprite.texture->texture_atlas_layout;
|
||||||
uint8_t row = sprite.texture_atlas_index / layout.columns;
|
uint8_t row = sprite.texture_atlas_index / layout.columns;
|
||||||
uint8_t column = sprite.texture_atlas_index % layout.columns;
|
uint8_t column = sprite.texture_atlas_index % layout.columns;
|
||||||
@@ -87,6 +92,25 @@ int main() {
|
|||||||
&srcrect, &dstrect);
|
&srcrect, &dstrect);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
world.system<ButtonInput const, Position, Sprite const, Fruit>("MoveSprites")
|
||||||
|
.term_at(0)
|
||||||
|
.singleton()
|
||||||
|
.each([](ButtonInput const &input, Position &pos, Sprite const &sprite,
|
||||||
|
Fruit) {
|
||||||
|
if (input.pressed.contains(SDLK_LEFT)) {
|
||||||
|
pos.x -= 1;
|
||||||
|
}
|
||||||
|
if (input.pressed.contains(SDLK_RIGHT)) {
|
||||||
|
pos.x += 1;
|
||||||
|
}
|
||||||
|
if (input.pressed.contains(SDLK_UP)) {
|
||||||
|
pos.y -= 1;
|
||||||
|
}
|
||||||
|
if (input.pressed.contains(SDLK_DOWN)) {
|
||||||
|
pos.y += 1;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
bool exit_gameloop = false;
|
bool exit_gameloop = false;
|
||||||
while (!exit_gameloop) {
|
while (!exit_gameloop) {
|
||||||
auto *input = world.get_mut<ButtonInput>();
|
auto *input = world.get_mut<ButtonInput>();
|
||||||
@@ -119,11 +143,6 @@ int main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Game Logic
|
// Game Logic
|
||||||
if (input->just_pressed.contains(SDLK_X))
|
|
||||||
spdlog::info("X pressed!");
|
|
||||||
|
|
||||||
if (input->just_released.contains(SDLK_X))
|
|
||||||
spdlog::info("X released!");
|
|
||||||
|
|
||||||
// Render
|
// Render
|
||||||
SDL_RenderClear(renderer);
|
SDL_RenderClear(renderer);
|
||||||
|
|||||||
Reference in New Issue
Block a user