Framerate independent logic updater, and started the sprite class

This commit is contained in:
plane000
2018-09-29 19:38:55 +01:00
parent 7962e94e7d
commit b944337044
11 changed files with 91 additions and 32 deletions

View File

@@ -3,8 +3,8 @@
Game::Game(std::string title, int width, int height, int targetFramerate, int targetUpdaterate)
: Renderer(title, width, height, targetFramerate) {
this->targetFramerate = targetFramerate;
this->targetUpdaterate = targetUpdaterate;
this->TargetMsPerFrame = targetFramerate;
this->TargetMsPerUpdate = targetUpdaterate;
std::cout << "Engine initialized" << std::endl;
}

View File

@@ -130,12 +130,15 @@
<ClCompile Include="main.cpp" />
<ClCompile Include="mathHelper.cpp" />
<ClCompile Include="renderer.cpp" />
<ClCompile Include="sprite.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="entity.h" />
<ClInclude Include="game.h" />
<ClInclude Include="mathHelper.h" />
<ClInclude Include="renderer.h" />
<ClInclude Include="sprite.h" />
<ClInclude Include="timer.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">

View File

@@ -6,6 +6,7 @@
<ClCompile Include="renderer.cpp" />
<ClCompile Include="game.cpp" />
<ClCompile Include="mathHelper.cpp" />
<ClCompile Include="sprite.cpp" />
</ItemGroup>
<ItemGroup>
<Filter Include="headers">
@@ -25,5 +26,11 @@
<ClInclude Include="mathHelper.h">
<Filter>headers</Filter>
</ClInclude>
<ClInclude Include="sprite.h">
<Filter>headers</Filter>
</ClInclude>
<ClInclude Include="timer.h">
<Filter>headers</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@@ -38,6 +38,17 @@ bool Entity::LoadTexture(std::string path) {
return true;
}
bool Entity::LoadTexture(SDL_Surface* image) {
m_texture = SDL_CreateTextureFromSurface(m_SDLRenderer, image);
if (m_texture == NULL) {
std::cout << "Unable to create texture SDL ERROR: " << SDL_GetError() << std::endl;
return false;
}
SDL_FreeSurface(image);
return true;
}
void Entity::SetDrawColour(Vec4 col) {
this->m_col = col;
// SDL_SetRenderDrawColor(m_SDLRenderer, col.x, col.y, col.z, col.w);

View File

@@ -22,7 +22,6 @@ enum struct PolyDrawType {
class Entity {
public:
Entity();
Entity(std::string name, SDL_Renderer* SDLRenderer); // Texture overload
Entity(std::string name, SDL_Renderer* SDLRenderer, PolyDrawType drawType); // Polygon overload
@@ -30,6 +29,7 @@ public:
PolyDrawType Drawtype = PolyDrawType::DRAW_DEFAULT;
bool LoadTexture(std::string path);
bool LoadTexture(SDL_Surface* image);
void SetDrawColour(Vec4 col);

View File

@@ -3,7 +3,9 @@
#include <string>
#include <SDL.h>
#include "renderer.h"
#include "timer.h"
#include "entity.h"
#include "sprite.h"
class Game : public Renderer {
public:
@@ -11,13 +13,12 @@ public:
void PollEvents();
int TargetMsPerFrame; // If 0, the engine will try as many as possibe, if 1, it will use vsync
int TargetMsPerUpdate; // If 0, the engine will try as many as possible
int MsPerFrame; // Current framerate
int MsPerUpdate; // Current updaterate
virtual ~Game();
private:
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
SDL_Event m_event;
};

View File

@@ -6,34 +6,28 @@
#define SCREEN_HEIGHT 600
int main(int argc, char** argv) {
Game game("Crumpet engine", SCREEN_WIDTH, SCREEN_HEIGHT, 0, 60);
Game game("Crumpet engine", SCREEN_WIDTH, SCREEN_HEIGHT, 0, 1000 / 60);
Timer timer;
Entity mario("mario", game.SDLRenderer);
mario.LoadTexture("/resources/mario.png");
Entity box("box", game.SDLRenderer, PolyDrawType::DRAW_FILLED_RECT);
box.SetDrawColour(Vec4(0xFF, 0x00, 0x00, 0xFF));
box.SetRect(Vec2(SCREEN_WIDTH / 4, SCREEN_HEIGHT / 4), Vec2(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2));
Entity outlineBox("bigger green box", game.SDLRenderer, PolyDrawType::DRAW_OUTLINE_RECT);
outlineBox.SetDrawColour(Vec4(0x00, 0xFF, 0x00, 0xFF));
outlineBox.SetRect(Vec2(SCREEN_WIDTH / 6, SCREEN_HEIGHT / 6), Vec2(SCREEN_WIDTH * 2 / 3, SCREEN_HEIGHT * 2 / 3));
Entity lineMesh("line mesh", game.SDLRenderer, PolyDrawType::DRAW_LINES);
lineMesh.SetDrawColour(Vec4(0x00, 0x00, 0xFF, 0xFF));
lineMesh.AddVecPoint(Vec4(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT));
Sprite sans("sans", game.SDLRenderer, SpriteType::SPRITE_ANIMATED);
while (!game.IsDisplayClosed()) {
game.PollEvents();
if (timer.GetTimeElapsed() >= game.TargetMsPerUpdate) {
std::cout << timer.GetTimeElapsed() << std::endl;
// game logic
timer.Tick();
}
game.RenderClear();
game.RenderEntity(&mario);
game.RenderEntity(&box);
game.RenderEntity(&outlineBox);
game.RenderEntity(&lineMesh);
game.RenderUpdate();
}

View File

@@ -1,7 +1,7 @@
#include "sprite.h"
Sprite::Sprite()
: Entity() {
Sprite::Sprite(std::string name, SDL_Renderer* SDLRenderer, SpriteType mode)
: Entity(name, SDLRenderer) {
}

View File

@@ -1,10 +1,28 @@
#pragma once
#include "entity.h";
#include <string>
#include <iostream>
#include <vector>
#include <SDL.h>
#include <SDL_image.h>
#include "entity.h"
enum struct SpriteType {
SPRITE_DEFAULT,
SPRITE_ANIMATED,
SPRITE_STATIC
};
class Sprite : public Entity {
public:
Sprite();
Sprite(std::string name, SDL_Renderer* SDLRenderer, SpriteType mode);
SpriteType Spritetype = SpriteType::SPRITE_DEFAULT;
bool LoadSpriteTextures(std::string path);
virtual ~Sprite();
private:
SDL_Texture *m_spriteSheetTexture;
};

9
crumpet-engine/timer.cpp Normal file
View File

@@ -0,0 +1,9 @@
#include "timer.h"
Timer::Timer() {
}
Timer::~Timer() {
}

16
crumpet-engine/timer.h Normal file
View File

@@ -0,0 +1,16 @@
#pragma once
#include <iostream>
#include <string>
#include <SDL.h>
class Timer {
public:
Timer() { pastTicks = SDL_GetTicks(); };
void Tick() { pastTicks = SDL_GetTicks(); this->ticks++; }
int GetTimeElapsed() { return SDL_GetTicks() - pastTicks; }
private:
int ticks = 0;
int pastTicks;
};