123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331 |
- /*
- * 2D Game Engine
- * Game.cpp: Base Game engine class
- * Based on pikuma.com 2D game engine in C++ and Lua course
- * Copyright (c) 2021 986-Studio. All Right Reserved
- *
- * Created by Manoël Trapier on 09/02/2021.
- */
- #include <SDL.h>
- #include <SDL_image.h>
- #include <glm/glm.hpp>
- #include <stdlib.h>
- #include <memory>
- #include <fstream>
- #include <Game.h>
- #include <Logger.h>
- #include <ECS.h>
- #include <Components/Animation.h>
- #include <Components/Transform.h>
- #include <Components/RigidBody.h>
- #include <Components/Sprite.h>
- #include <Components/BoxCollider.h>
- #include <Components/KeyboardControl.h>
- #include <Components/CameraFollow.h>
- #include <Components/ProjectileEmitter.h>
- #include <Components/Heakth.h>
- #include <Components/TextLabel.h>
- #include <Systems/Movement.h>
- #include <Systems/Render.h>
- #include <Systems/Animation.h>
- #include <Systems/Collision.h>
- #include <Systems/Damage.h>
- #include <Systems/KeyboardMovement.h>
- #include <Systems/CameraMovement.h>
- #include <Systems/ProjectileEmit.h>
- #include <Systems/ProjectileLifeCycle.h>
- #include <Systems/RenderText.h>
- #include <Systems/RenderHealth.h>
- uint32_t Game::windowsWidth = 0;
- uint32_t Game::windowsHeight = 0;
- uint32_t Game::mapWidth = 0;
- uint32_t Game::mapHeight = 0;
- Game::Game()
- {
- this->isRunning = false;
- this->isDebug = false;
- this->millisecsPerviousFrame = 0;
- this->registry = std::make_unique<Registry>();
- this->assetStore = std::make_unique<AssetStore>();
- this->eventBus = std::make_unique<EventBus>();
- }
- Game::~Game()
- {
- }
- void Game::Initialize()
- {
- int ret;
- SDL_DisplayMode displayMode;
- ret = SDL_Init(SDL_INIT_EVERYTHING);
- if (ret)
- {
- Logger::Critical("SDL Initialisation error.");
- return;
- }
- ret = TTF_Init();
- if (ret)
- {
- Logger::Critical("SDL_ttf Initialisation error.");
- return;
- }
- SDL_GetCurrentDisplayMode(0, &displayMode);
- Game::windowsWidth = 800; //displayMode.w;
- Game::windowsHeight = 600; //displayMode.h;
- this->window = SDL_CreateWindow(NULL, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
- Game::windowsWidth, Game::windowsHeight, SDL_WINDOW_BORDERLESS);
- if (!window)
- {
- Logger::Critical("SDL window creation error.");
- return;
- }
- this->renderer = SDL_CreateRenderer(this->window, -1, 0);
- if (!renderer)
- {
- Logger::Critical("SDL renderer creation error.");
- return;
- }
- //SDL_SetWindowFullscreen(this->window, SDL_WINDOW_FULLSCREEN);
- this->camera.x = 0;
- this->camera.y = 0;
- this->camera.w = Game::windowsWidth;
- this->camera.h = Game::windowsHeight;
- this->isRunning = true;
- }
- void Game::Destroy()
- {
- if (this->renderer)
- {
- SDL_DestroyRenderer(this->renderer);
- }
- if (this->window)
- {
- SDL_DestroyWindow(this->window);
- }
- SDL_Quit();
- }
- void Game::Run()
- {
- this->Setup();
- #if 0
- for(int i = 0 ; i < 100; i++)
- {
- this->ProcessInput();
- this->Update();
- this->Render();
- }
- #else
- while(this->isRunning)
- {
- this->ProcessInput();
- this->Update();
- this->Render();
- }
- #endif
- }
- void Game::LoadLevel(int level)
- {
- this->registry->addSystem<MovementSystem>();
- this->registry->addSystem<RenderSystem>();
- this->registry->addSystem<AnimationSystem>();
- this->registry->addSystem<CollisionSystem>();
- this->registry->addSystem<DamageSystem>();
- this->registry->addSystem<KeyboardMovementSystem>();
- this->registry->addSystem<CameraMovementSystem>();
- this->registry->addSystem<ProjectileEmitSystem>();
- this->registry->addSystem<ProjectileLifeCycleSystem>();
- this->registry->addSystem<RenderTextSystem>();
- this->registry->addSystem<RenderHealthSystem>();
- this->assetStore->addTexture(this->renderer, "tank-image", "assets/images/tank-panther-right.png");
- this->assetStore->addTexture(this->renderer, "truck-image", "assets/images/truck-ford-right.png");
- this->assetStore->addTexture(this->renderer, "chopper-image", "assets/images/chopper-spritesheet.png");
- this->assetStore->addTexture(this->renderer, "main-tileset", "assets/tilemaps/jungle.png");
- this->assetStore->addTexture(this->renderer, "radar-image", "assets/images/radar.png");
- this->assetStore->addTexture(this->renderer, "bullet-image", "assets/images/bullet.png");
- this->assetStore->addFont("charriot-font", "assets/fonts/charriot.ttf", 20);
- this->assetStore->addFont("arial-font", "assets/fonts/charriot.ttf", 20);
- this->assetStore->addFont("pico8-font", "assets/fonts/charriot.ttf", 10);
- Logger::Debug("struct SpriteComponent size is %d", sizeof(struct SpriteComponent));
- // jungle.map
- int tileSize = 32;
- double tileScale = 2.0;
- int mapNumCols = 25;
- int mapNumRows = 20;
- std::fstream mapFile;
- mapFile.open("assets/tilemaps/jungle.map");
- assert(mapFile.is_open());
- for (int y = 0; y < mapNumRows; y++)
- {
- for (int x = 0; x < mapNumCols; x++)
- {
- char ch[2] = {0, 0};
- mapFile.get(ch[0]);
- int srcRectY = atoi(ch) * tileSize;
- mapFile.get(ch[0]);
- int srcRectX = atoi(ch) * tileSize;
- mapFile.ignore();
- Entity tile = registry->createEntity();
- tile.addComponent<TransformComponent>(glm::vec2(x * (tileScale * tileSize), y * (tileScale * tileSize)), glm::vec2(tileScale, tileScale), 0.0);
- tile.addComponent<SpriteComponent>("main-tileset", 0, tileSize, tileSize, false, srcRectX, srcRectY);
- tile.group("tiles");
- }
- }
- mapFile.close();
- Game::mapWidth = mapNumCols * tileSize * tileScale;
- Game::mapHeight = mapNumRows * tileSize * tileScale;
- Entity chopper = this->registry->createEntity();
- chopper.addComponent<TransformComponent>(glm::vec2(240, 110), glm::vec2(1, 1), 0);
- chopper.addComponent<SpriteComponent>("chopper-image", 1, 32, 32);
- chopper.addComponent<RigidBodyComponent>(glm::vec2(0, 0));
- chopper.addComponent<BoxColliderComponent>(32, 32);
- chopper.addComponent<AnimationComponent>(2, 15, true);
- chopper.addComponent<KeyboardControlComponent>(glm::vec2(0, -80), glm::vec2(80, 0),
- glm::vec2(0, 80), glm::vec2(-80, 0));
- chopper.addComponent<ProjectileEmitterComponent>(glm::vec2(180, 180), 0, 10000, 10, true);
- chopper.addComponent<HealthComponent>(100);
- chopper.addComponent<CameraFollowComponent>();
- chopper.tag("player");
- Entity radar = this->registry->createEntity();
- radar.addComponent<TransformComponent>(glm::vec2(this->windowsWidth - 75, 10), glm::vec2(1, 1), 0);
- radar.addComponent<RigidBodyComponent>(glm::vec2(0, 0));
- radar.addComponent<SpriteComponent>("radar-image", 3, 64, 64, true);
- radar.addComponent<AnimationComponent>(8, 5, true);
- Entity tank = this->registry->createEntity();
- tank.addComponent<TransformComponent>(glm::vec2(500, 500), glm::vec2(1, 1), 0);
- tank.addComponent<RigidBodyComponent>(glm::vec2(0, 0));
- tank.addComponent<SpriteComponent>("tank-image", 1, 32, 32);
- tank.addComponent<BoxColliderComponent>(32, 32);
- tank.addComponent<ProjectileEmitterComponent>(glm::vec2(100, 0), 3000, 5000, 10, false);
- tank.addComponent<HealthComponent>(100);
- tank.group("enemies");
- Entity truck = this->registry->createEntity();
- truck.addComponent<TransformComponent>(glm::vec2(120, 500), glm::vec2(1, 1), 0);
- truck.addComponent<RigidBodyComponent>(glm::vec2(0, 0));
- truck.addComponent<SpriteComponent>("truck-image", 2, 32, 32);
- truck.addComponent<BoxColliderComponent>(32, 32);
- truck.addComponent<ProjectileEmitterComponent>(glm::vec2(0, -100), 2000, 5000, 10, false);
- truck.addComponent<HealthComponent>(100);
- truck.group("enemies");
- SDL_Color green = { 64, 255, 127, 255 };
- Entity label = this->registry->createEntity();
- label.addComponent<TextLabelComponent>(glm::vec2(this->windowsWidth/2 - 60, 10), "SUPER CHOPPER BROS 1.0", "charriot-font", green);
- }
- void Game::Setup()
- {
- LoadLevel(1);
- }
- void Game::Update()
- {
- uint32_t timeToWait = MILLISECS_PER_FRAME - (SDL_GetTicks() - this->millisecsPerviousFrame);
- if (timeToWait > 0 && timeToWait <= MILLISECS_PER_FRAME)
- {
- SDL_Delay(timeToWait);
- }
- double deltaTime = (SDL_GetTicks() - this->millisecsPerviousFrame) / 1000.0;
- this->millisecsPerviousFrame = SDL_GetTicks();
- eventBus->reset();
- registry->getSystem<DamageSystem>().subscriptToEvents(eventBus);
- registry->getSystem<KeyboardMovementSystem>().subscriptToEvents(eventBus);
- registry->getSystem<ProjectileEmitSystem>().subscriptToEvents(eventBus);
- registry->update();
- registry->getSystem<MovementSystem>().update(deltaTime);
- registry->getSystem<AnimationSystem>().update();
- registry->getSystem<CollisionSystem>().update(this->eventBus);
- registry->getSystem<ProjectileEmitSystem>().update(this->registry);
- registry->getSystem<ProjectileLifeCycleSystem>().update();
- registry->getSystem<CameraMovementSystem>().update(this->camera);
- }
- void Game::ProcessInput()
- {
- SDL_Event event;
- while(SDL_PollEvent(&event))
- {
- switch(event.type)
- {
- case SDL_QUIT:
- this->isRunning = false;
- break;
- case SDL_KEYDOWN:
- if (event.key.keysym.sym == SDLK_ESCAPE)
- {
- this->isRunning = false;
- }
- else if (event.key.keysym.sym == SDLK_d)
- {
- this->isDebug = !this->isDebug;
- }
- else
- {
- this->eventBus->emitEvent<KeyPressedEvent>(event.key.keysym.sym);
- }
- break;
- default:
- break;
- }
- }
- }
- void Game::Render()
- {
- SDL_SetRenderDrawColor(this->renderer, 21, 21, 21, 255);
- SDL_RenderClear(this->renderer);
- registry->getSystem<RenderSystem>().update(this->renderer, this->assetStore, this->camera);
- registry->getSystem<RenderTextSystem>().update(this->renderer, this->assetStore, this->camera);
- registry->getSystem<RenderHealthSystem>().update(this->renderer, this->assetStore, this->camera);
- if (this->isDebug)
- {
- registry->getSystem<CollisionSystem>().debugRender(this->renderer, this->camera);
- }
- SDL_RenderPresent(this->renderer);
- }
|