|
| 1 | +#include <SDL2/SDL.h> |
| 2 | +#include <ecs/registry.hpp> |
| 3 | +#include <ecs/zipper.hpp> |
| 4 | +#include <iostream> |
| 5 | +#include <chrono> |
| 6 | + |
| 7 | +struct position { |
| 8 | + float x, y; |
| 9 | +}; |
| 10 | + |
| 11 | +struct velocity { |
| 12 | + float vx, vy; |
| 13 | +}; |
| 14 | + |
| 15 | +struct drawable { |
| 16 | + SDL_Texture* texture; |
| 17 | + SDL_Rect rect; |
| 18 | +}; |
| 19 | + |
| 20 | +void position_system(ecs::Registry<position, velocity, drawable>& reg) { |
| 21 | + auto& positions = reg.get_components<position>(); |
| 22 | + auto& velocities = reg.get_components<velocity>(); |
| 23 | + |
| 24 | + for (auto&& [pos, vel] : ecs::Zipper(positions, velocities)) { |
| 25 | + if (pos && vel) { |
| 26 | + pos->x += vel->vx; |
| 27 | + pos->y += vel->vy; |
| 28 | + } |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +void draw_system(ecs::Registry<position, velocity, drawable>& reg, SDL_Renderer* renderer) { |
| 33 | + auto& drawables = reg.get_components<drawable>(); |
| 34 | + auto& positions = reg.get_components<position>(); |
| 35 | + |
| 36 | + for (auto&& [draw, pos] : ecs::Zipper(drawables, positions)) { |
| 37 | + if (draw && pos) { |
| 38 | + draw->rect.x = static_cast<int>(pos->x); |
| 39 | + draw->rect.y = static_cast<int>(pos->y); |
| 40 | + SDL_RenderCopy(renderer, draw->texture, nullptr, &draw->rect); |
| 41 | + } |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +int main() { |
| 46 | + SDL_Init(SDL_INIT_VIDEO); |
| 47 | + SDL_Window* window = SDL_CreateWindow("ECS with SDL2", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_SHOWN); |
| 48 | + SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); |
| 49 | + |
| 50 | + ecs::Registry<position, velocity, drawable> reg; |
| 51 | + |
| 52 | + reg.register_component<position>(); |
| 53 | + reg.register_component<velocity>(); |
| 54 | + reg.register_component<drawable>(); |
| 55 | + |
| 56 | + SDL_Texture* texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, 50, 50); |
| 57 | + SDL_SetRenderTarget(renderer, texture); |
| 58 | + SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); |
| 59 | + SDL_RenderClear(renderer); |
| 60 | + SDL_SetRenderTarget(renderer, nullptr); |
| 61 | + |
| 62 | + auto entity1 = reg.spawn_entity(); |
| 63 | + reg.add_component(entity1, position{100.0f, 100.0f}); |
| 64 | + reg.add_component(entity1, velocity{0.2f, 0.15f}); |
| 65 | + reg.add_component(entity1, drawable{texture, {0, 0, 50, 50}}); |
| 66 | + |
| 67 | + auto entity2 = reg.spawn_entity(); |
| 68 | + reg.add_component(entity2, position{200.0f, 300.0f}); |
| 69 | + reg.add_component(entity2, velocity{-0.1f, -0.1f}); |
| 70 | + reg.add_component(entity2, drawable{texture, {0, 0, 50, 50}}); |
| 71 | + |
| 72 | + reg.add_system(position_system); |
| 73 | + reg.add_system([&](ecs::Registry<position, velocity, drawable>& reg) { |
| 74 | + draw_system(reg, renderer); |
| 75 | + }); |
| 76 | + |
| 77 | + const int FPS = 60; |
| 78 | + const int frame_delay = 1000 / FPS; |
| 79 | + |
| 80 | + bool running = true; |
| 81 | + SDL_Event event; |
| 82 | + |
| 83 | + while (running) { |
| 84 | + auto frame_start = SDL_GetTicks(); |
| 85 | + |
| 86 | + while (SDL_PollEvent(&event)) { |
| 87 | + if (event.type == SDL_QUIT) { |
| 88 | + running = false; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); |
| 93 | + SDL_RenderClear(renderer); |
| 94 | + |
| 95 | + reg.run_systems(); |
| 96 | + |
| 97 | + SDL_RenderPresent(renderer); |
| 98 | + |
| 99 | + auto frame_time = SDL_GetTicks() - frame_start; |
| 100 | + |
| 101 | + if (frame_delay > frame_time) { |
| 102 | + SDL_Delay(frame_delay - frame_time); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + SDL_DestroyTexture(texture); |
| 107 | + SDL_DestroyRenderer(renderer); |
| 108 | + SDL_DestroyWindow(window); |
| 109 | + SDL_Quit(); |
| 110 | + |
| 111 | + return 0; |
| 112 | +} |
0 commit comments