Renderer and entity class
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
#include "game.h"
|
||||
|
||||
Game::Game(std::string title, int width, int height)
|
||||
Game::Game(std::string title, int width, int height, int targetFramerate, int targetUpdaterate)
|
||||
: m_display(title, width, height) {
|
||||
|
||||
this->targetFramerate = targetFramerate;
|
||||
this->targetUpdaterate = targetUpdaterate;
|
||||
|
||||
std::cout << "Engine initialized" << std::endl;
|
||||
}
|
||||
|
||||
@@ -18,8 +21,16 @@ bool Game::IsDisplayClosed() {
|
||||
return m_display.IsClosed();
|
||||
}
|
||||
|
||||
Display Game::GetDisplay() {
|
||||
Renderer Game::GetDisplay() {
|
||||
return m_display;
|
||||
}
|
||||
|
||||
Game::~Game() {}
|
||||
void Game::PollEvents() {
|
||||
while (SDL_PollEvent(&m_event) != 0)
|
||||
if (m_event.type == SDL_QUIT)
|
||||
this->CloseDisplay();
|
||||
}
|
||||
|
||||
Game::~Game() {
|
||||
|
||||
}
|
||||
|
||||
@@ -125,14 +125,14 @@
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="display.cpp" />
|
||||
<ClCompile Include="entity.cpp" />
|
||||
<ClCompile Include="renderer.cpp" />
|
||||
<ClCompile Include="game.cpp" />
|
||||
<ClCompile Include="main.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="display.h" />
|
||||
<ClInclude Include="entity.h" />
|
||||
<ClInclude Include="renderer.h" />
|
||||
<ClInclude Include="game.h" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup>
|
||||
<ClCompile Include="main.cpp" />
|
||||
<ClCompile Include="display.cpp" />
|
||||
<ClCompile Include="game.cpp" />
|
||||
<ClCompile Include="renderer.cpp" />
|
||||
<ClCompile Include="entity.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
@@ -12,10 +12,10 @@
|
||||
</Filter>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="display.h">
|
||||
<ClInclude Include="game.h">
|
||||
<Filter>headers</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="game.h">
|
||||
<ClInclude Include="renderer.h">
|
||||
<Filter>headers</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="entity.h">
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
#include "display.h"
|
||||
|
||||
Display::Display(std::string title, int width, int height) {
|
||||
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
|
||||
std::cout << "SDL could not initialize, SDL ERROR: " << SDL_GetError() << std::endl;
|
||||
}
|
||||
|
||||
m_window = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_SHOWN);
|
||||
m_renderer = SDL_CreateRenderer(m_window, -1, SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED);
|
||||
SDL_SetRenderDrawColor(m_renderer, 123, 122, 0, 255);
|
||||
|
||||
isClosed = false;
|
||||
}
|
||||
|
||||
void Display::Update() {
|
||||
|
||||
}
|
||||
|
||||
bool Display::IsClosed() {
|
||||
return isClosed;
|
||||
}
|
||||
|
||||
void Display::Close() {
|
||||
isClosed = true;
|
||||
}
|
||||
|
||||
Display::~Display() {
|
||||
isClosed = true;
|
||||
SDL_DestroyWindow(m_window);
|
||||
SDL_Quit();
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <SDL.h>
|
||||
|
||||
class Display {
|
||||
public:
|
||||
Display(std::string title, int width, int height);
|
||||
|
||||
void Update();
|
||||
|
||||
bool IsClosed();
|
||||
void Close();
|
||||
virtual ~Display();
|
||||
private:
|
||||
SDL_Window *m_window;
|
||||
SDL_Renderer *m_renderer;
|
||||
|
||||
bool isClosed;
|
||||
};
|
||||
|
||||
@@ -1,19 +1,29 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include "display.h"
|
||||
#include "renderer.h"
|
||||
#include "entity.h"
|
||||
|
||||
class Game {
|
||||
public:
|
||||
Game(std::string title, int width, int height);
|
||||
Game(std::string title, int width, int height, int targetFramerate, int targetUpdaterate);
|
||||
|
||||
void UpdateDisplay();
|
||||
void CloseDisplay();
|
||||
bool IsDisplayClosed();
|
||||
|
||||
Display GetDisplay();
|
||||
Renderer GetDisplay();
|
||||
|
||||
void PollEvents();
|
||||
|
||||
virtual ~Game();
|
||||
private:
|
||||
Display m_display;
|
||||
int targetFramerate; // If 0, the engine will try as many as possibe, if 1, it will use vsync
|
||||
int targetUpdaterate; // If 0, the engine will try as many as possible
|
||||
|
||||
int framerate; // Current framerate
|
||||
int updaterate; // Current updaterate
|
||||
|
||||
Renderer m_display;
|
||||
SDL_Event m_event;
|
||||
};
|
||||
|
||||
@@ -2,18 +2,17 @@
|
||||
|
||||
#undef main
|
||||
|
||||
#define SCREEN_WIDTH 640
|
||||
#define SCREEN_HEIGHT 480
|
||||
#define SCREEN_WIDTH 800
|
||||
#define SCREEN_HEIGHT 600
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
Game game("Crumpet engine", SCREEN_WIDTH, SCREEN_HEIGHT);
|
||||
Game game("Crumpet engine", SCREEN_WIDTH, SCREEN_HEIGHT, 0, 60);
|
||||
|
||||
|
||||
|
||||
SDL_Event e;
|
||||
while (!game.IsDisplayClosed()) {
|
||||
|
||||
while (SDL_PollEvent(&e) != 0)
|
||||
if (e.type == SDL_QUIT)
|
||||
game.CloseDisplay();
|
||||
game.PollEvents();
|
||||
|
||||
|
||||
game.UpdateDisplay();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user