Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
No results found
Show changes
Commits on Source (24)
Showing
with 397 additions and 14 deletions
File added
assets/animated.png

839 B

assets/arrow.png

1.44 KiB

assets/background.png

22.1 KiB

assets/hello-world.bmp

91.2 KiB

assets/person.png

1.49 KiB

assets/sq.bmp

7.36 KiB

assets/texture_test.png

839 B

assets/water_animated.png

11.4 KiB

#ifndef __COLOUR_H__
#define __COLOUR_H__
#include <cstdint>
struct RGBA{
uint8_t r;
uint8_t g;
uint8_t b;
uint8_t a;
};
class Colour
{
public:
Colour();
Colour( uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255 );
~Colour();
void brighten( uint8_t delta );
void darken( uint8_t delta );
RGBA getRGBA();
private:
RGBA _col;
};
#endif //__COLOUR_H__
\ No newline at end of file
#ifndef __COMPONENTS__FONTFILE_H__
#define __COMPONENTS__FONTFILE_H__
#include <SDL_image.h>
#include <SDL_ttf.h>
#include "components/cTexture.h"
#include "colour.h"
class cFontfile : public cTexture
{
static bool ttf_isInit;
public:
cFontfile();
void renderText( std::string str );
bool loadFromFile( std::string path );
void setColour( Colour colour );
void setColour( uint8_t red, uint8_t green, uint8_t blue );
private:
TTF_Font* _font = NULL;
Colour _colour = Colour(0, 0, 0);
SDL_Color _col = { 0xff, 0xff, 0x00 };
uint8_t _fontsize = 28;
};
#endif // __COMPONENTS__FONTFILE_H__
\ No newline at end of file
#ifndef __COMPONENTS__TEXTURE_H__
#define __COMPONENTS__TEXTURE_H__
#ifndef console
#include <SDL.h>
#endif
#include <cstdio>
#include <string>
#include "components/components-generic.h"
#include "window.h"
#include "logger.h"
#include "colour.h"
#include "maths/vec2.h"
class cTexture : public CComponentGeneric
{
public:
cTexture();
~cTexture();
bool loadFromFile( std::string path );
void free();
void render(
SDL_Renderer *renderer,
Vec2 pt,
SDL_Rect *clip = NULL,
double angle = 0,
SDL_Point* center = NULL,
SDL_RendererFlip flip = SDL_FLIP_NONE
);
int getWidth(){ return this->_width; }
int getHeight(){ return this->_height; }
void setColour( Colour colour );
void setColour( uint8_t red, uint8_t green, uint8_t blue );
void setBlendMode( SDL_BlendMode blending );
void setAlpha( uint8_t alpha );
protected:
Logger &_logger;
SDL_Texture* _texture;
uint16_t _width;
uint16_t _height;
};
class cFontfile;
#endif // __COMPONENTS__TEXTURE_H__
#ifndef __ENTITIES__BUTTON
#define __ENTITIES__BUTTON
#include <SDL.h>
#include "entities/entity.h"
#include "maths/maths.h"
#include "components/cTexture.h"
class eButton : public Entity
{
public:
void setPosition( Vec2 pos );
void handleEvent( SDL_Event* evt );
void render();
private:
Vec2 _pos;
cTexture _sprite;
};
#endif // __ENTITIES__BUTTON
......@@ -7,6 +7,10 @@
#include <vector>
#include <chrono>
#include <ctime>
#ifndef console
#include <SDL.h>
#endif
// Return
......@@ -14,14 +18,13 @@
#include "entity-manager.h"
#include "game-state.h"
#include "scene.h"
#include "window.h"
#include "asset.h"
#include "save-manager.h"
// #include "window.h"
#ifdef console
#endif
class Window {};
class Window;
class GameEngine
{
......@@ -32,6 +35,7 @@ class GameEngine
void run();
void update();
void close();
void clean();
Scene* currentScene();
void changeScene( std::string label, Scene *scene );
void changeScene( std::string label );
......@@ -53,6 +57,8 @@ class GameEngine
bool _paused = false;
Window* _window;
// std::vector<Asset> _assets;
// std::map< std::string, Scene > _scenes;
std::vector<Asset> _assets; /** @todo : move this to scene */
std::string _currScene;
......
......@@ -4,6 +4,8 @@
#include <iostream>
#include <cstdint>
#define SDL_MAIN_HANDLED
#ifdef __TEST_RUNNER__
#ifndef DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
......@@ -12,7 +14,6 @@
#include "testlist.hpp"
#endif // __TEST_RUNNER__
enum GenerationMethod
{
Random,
......@@ -30,5 +31,8 @@ enum GenerationMethod
#include "entities/entity.h"
#include "maths/maths.h"
#include "entity-manager.h"
#include "components/cTexture.h"
#include "window.h"
#endif // MAIN_H
#ifndef __WINDOW_H__
#define __WINDOW_H__
#ifndef console
#include <SDL.h>
#include <SDL_image.h>
#endif
#include <cstdio>
#include <cstring>
#include <string>
#include "logger.h"
#include "colour.h"
#include "maths/vec2.h"
// #include "components/cFontfile.h"
// #include "components/cTexture.h"
class cFontfile;
class cTexture;
const uint16_t SCREEN_HEIGHT = 600;
const uint16_t SCREEN_WIDTH = 800;
//Key press surfaces constants
enum KeyPressSurfaces
{
KEY_PRESS_SURFACE_DEFAULT,
KEY_PRESS_SURFACE_UP,
KEY_PRESS_SURFACE_DOWN,
KEY_PRESS_SURFACE_LEFT,
KEY_PRESS_SURFACE_RIGHT,
KEY_PRESS_SURFACE_TOTAL
};
class Window
{
public:
static SDL_Renderer* renderer;
static bool sdlIsInit;
static RGBA KeyColour;
static RGBA RenderColour;
Window();
~Window();
void loadMedia( std::string path );
void loadMedia();
void update();
void processInput();
SDL_Texture* loadTexture( std::string path );
bool isClosed() { return this->_toClose; }
void spawn();
private:
bool _toClose;
bool _hasInit;
SDL_Surface* loadSurface( std::string path );
Logger& _logger;
SDL_Window* _window;
size_t _frame = 0;
SDL_Texture* _activeTexture;
Colour _screenModulation;
cTexture* _background;
cTexture* _character;
cTexture* _arrow;
cTexture* _textureSprite;
cFontfile* _fontFile;
SDL_Rect _sprites[4];
uint8_t _alpha = 0xff;
cTexture* _water;
const uint8_t _waterFrames = 4;
SDL_Rect _waterTicks[4];
SDL_Renderer* _renderer;
SDL_Surface* _screenSurface;
SDL_Surface* _renderSurface;
SDL_Rect _drawRect( int x, int y, uint16_t width, uint16_t height );
void _drawLine( int16_t x1, int16_t y1, int16_t x2, int16_t y2 );
// void _drawLine( Vec2 from, Vec2 to );
void _drawPoint( Vec2 point );
// void _drawPoint( int16_t x, int16_t y );
void _drawHex( Vec2 point, uint16_t radius );
SDL_Rect _createViewport( int16_t x, int16_t y, int16_t w, int16_t h );
void initialise();
void clean();
};
#endif // __WINDOW_H__
......@@ -5,14 +5,18 @@ VERSION_PATCH = 0
ifeq ($(OS),Windows_NT)
CXX = g++ # or your Windows compiler
CXXFLAGS = -std=c++17 -Wall -Wreorder
RM = del /Q
SDL_INCLUDE = C:/dev/SDL2/x86_64-w64-mingw32/include/SDL2
SDL_LIBRARY = C:/dev/SDL2/x86_64-w64-mingw32/lib
else
UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Linux)
CXX = g++
# CXX = clang
CXXFLAGS = -std=c++17 -Wall -Wreorder
RM = rm -f
SDL_INCLUDE =
SDL_LIBRARY =
endif
# Add more conditions for other platforms as needed
endif
......@@ -49,11 +53,13 @@ TEST_BUILD_DIR = $(BUILD_DIR)/.test
BIN_DIR = bin
# SDL Inclusions
SDL_INCLUDE = -IC:/dev/SDL2/x86_64-w64-mingw32/include/SDL2
SDL_LIBRARY = -LC:/dev/SDL2/x86_64-w64-mingw32/lib
SDL_OPTIONS = -lSDL2main -lSDL2
SDL_INCLUDE = C:/dev/SDL2/x86_64-w64-mingw32/include/SDL2
SDL_LIBRARY = C:/dev/SDL2/x86_64-w64-mingw32/lib
SDL_LINKER_OPTIONS = -lmingw32 -lSDL2main -lSDL2 -lSDL2_image -lSDL2_ttf -lSDL2_mixer
# SDL_OPTIONS = -lmingw32 -lSDL2main -lSDL2
LDFLAGS += $(SDL_LINKER_OPTIONS)
# Source files
SRC_FILES = $(wildcard $(SRC_DIR)/**/*.cpp $(SRC_DIR)/*.cpp )
......@@ -78,22 +84,22 @@ test: $(TEST_TARGET)
game: $(TARGET)
$(TARGET): $(OBJ_FILES)
$(CXX) $(CXXFLAGS) -o $(TARGET) $(OBJ_FILES) $(SDL_LIBRARY) $(SDL_OPTIONS)
$(CXX) $(CXXFLAGS) -o $(TARGET) $(OBJ_FILES) -L$(SDL_LIBRARY) $(LDFLAGS)
$(TEST_TARGET): $(TEST_OBJ_FILES) $(TEST_SRC_OBJ_FILES)
$(CXX) $(CXXFLAGS) -o $(TEST_TARGET) $(TEST_OBJ_FILES) $(TEST_SRC_OBJ_FILES) $(SDL_LIBRARY) $(SDL_OPTIONS)
$(CXX) $(CXXFLAGS) -o $(TEST_TARGET) $(TEST_OBJ_FILES) $(TEST_SRC_OBJ_FILES) -L$(SDL_LIBRARY) $(SDL_OPTIONS) $(LDFLAGS)
$(TEST_BUILD_DIR)/%.src.o: $(SRC_DIR)/%.cpp ${INC_DIR}/%.h
@mkdir -p $(@D)
$(CXX) $(CXXFLAGS) $(TEST_DEFINES) $(LDFLAGS) -I$(INC_DIR) -I$(TEST_HEADER) -I$(TEST_DIR) -c -o $@ $<
$(CXX) $(CXXFLAGS) $(TEST_DEFINES) $(LDFLAGS) -I$(SDL_INCLUDE) -I$(INC_DIR) -I$(TEST_HEADER) -I$(TEST_DIR) -c -o $@ $<
$(TEST_BUILD_DIR)/%.test.o: $(SRC_DIR)/%.test.hpp $(SRC_DIR)/%.cpp ${INC_DIR}/%.h
@mkdir -p $(@D)
$(CXX) $(CXXFLAGS) $(TEST_DEFINES) $(LDFLAGS) -I$(INC_DIR) -I$(TEST_HEADER) -c -o $@ $<
$(CXX) $(CXXFLAGS) $(TEST_DEFINES) $(LDFLAGS) -I$(SDL_INCLUDE) -I$(INC_DIR) -I$(TEST_HEADER) -c -o $@ $<
$(BUILD_DIR)/%.o: $(SRC_DIR)/%.cpp ${INC_DIR}/%.h
@mkdir -p $(@D)
$(CXX) $(CXXFLAGS) $(LDFLAGS) -I$(INC_DIR) -c -o $@ $<
$(CXX) $(CXXFLAGS) $(LDFLAGS) -I${SDL_INCLUDE} -I$(INC_DIR) -c -o $@ $<
clean:
rm -rf $(TEST_BUILD_DIR)/* $(TEST_TARGET)
......
#include "colour.h"
Colour::Colour( )
: _col( {0x00, 0x00, 0x00, 0x00} )
{}
Colour::Colour( uint8_t r, uint8_t g, uint8_t b, uint8_t a )
: _col( {r,g,b,a} )
{}
Colour::~Colour()
{}
RGBA Colour::getRGBA() { return this->_col; }
void Colour::brighten( uint8_t delta )
{
if( this->_col.r < 0xFF ) this->_col.r += delta;
if( this->_col.g < 0xFF ) this->_col.g += delta;
if( this->_col.b < 0xFF ) this->_col.b += delta;
}
void Colour::darken( uint8_t delta )
{
if( this->_col.r > 0 ) this->_col.r -= delta;
if( this->_col.g > 0 ) this->_col.g -= delta;
if( this->_col.b > 0 ) this->_col.b -= delta;
}
\ No newline at end of file
#include "components/cFontfile.h"
bool cFontfile::ttf_isInit = false;
cFontfile::cFontfile()
: cTexture()
{
if( ! cFontfile::ttf_isInit )
{
if( TTF_Init() == -1 )
{ printf( "SDL_ttf could not initialize! SDL_ttf Error: %s\n", TTF_GetError() ); }
else { cFontfile::ttf_isInit = true; }
}
}
bool cFontfile::loadFromFile( std::string path )
{
this->_logger.info( "Loading Font: "+path );
this->free();
SDL_Texture* texture = NULL;
this->_font = TTF_OpenFont( path.c_str(), this->_fontsize );
if( this->_font == NULL )
{ printf( "Failed to load lazy font! SDL_ttf Error: %s\n", TTF_GetError() ); }
else
{
}
return texture != NULL;
}
void cFontfile::renderText( std::string str )
{
free();
SDL_Surface* textSurface = TTF_RenderText_Solid( this->_font, str.c_str(), this->_col );
if( textSurface == NULL )
{ /* error */ return; }
this->_texture = SDL_CreateTextureFromSurface( Window::renderer, textSurface );
if( ! this->_texture )
{ /* error */ return; }
this->_width = textSurface->w;
this->_height = textSurface->h;
SDL_FreeSurface( textSurface );
}
void cFontfile::setColour( Colour colour ) { this->_colour = colour; }
void cFontfile::setColour( uint8_t red, uint8_t green, uint8_t blue ) {
this->_colour = Colour( red, green, blue );
}
\ No newline at end of file
#include "components/cTexture.h"
cTexture::cTexture()
: _texture( NULL ),
_logger( Logger::instance() ),
_width( 0 ), _height( 0 )
{}
cTexture::~cTexture(){ this->free(); }
bool cTexture::loadFromFile( std::string path )
{
this->free();
SDL_Texture* texture = NULL;
SDL_Surface* surface = IMG_Load( path.c_str() );
if( surface == NULL )
{ printf( "Unable to load image %s! SDL_image Error: %s\n", path.c_str(), IMG_GetError() ); }
else
{
SDL_SetColorKey( surface, SDL_TRUE, SDL_MapRGB( surface->format, 0xFF, 0x00, 0xFF ) );
texture = SDL_CreateTextureFromSurface( Window::renderer, surface );
if( texture == NULL )
{ printf( "Unable to create texture from %s! SDL Error: %s\n", path.c_str(), SDL_GetError() ); }
else
{
this->_width = surface->w;
this->_height = surface->h;
}
SDL_FreeSurface( surface );
}
this->_texture = texture;
this->setBlendMode( SDL_BLENDMODE_BLEND );
return texture != NULL;
}
void cTexture::free()
{
if( this->_texture != NULL )
{
_texture = NULL;
_width = 0;
_height = 0;
}
}
void cTexture::setColour( Colour colour ) { this->setColour( colour.getRGBA().r, colour.getRGBA().g, colour.getRGBA().b ); }
void cTexture::setColour( uint8_t red, uint8_t green, uint8_t blue ) { SDL_SetTextureColorMod( this->_texture, red, green, blue ); }
void cTexture::setBlendMode( SDL_BlendMode blending ) { SDL_SetTextureBlendMode( this->_texture, blending ); }
void cTexture::setAlpha( uint8_t alpha ) { SDL_SetTextureAlphaMod( this->_texture, alpha ); }
void cTexture::render( SDL_Renderer *renderer, Vec2 pt, SDL_Rect* clip, double angle, SDL_Point* center, SDL_RendererFlip flip )
{
if( this->_width && this->_height )
{
SDL_Rect quad = { (int)pt.x, (int)pt.y, this->_width, this->_height };
if( clip != NULL )
{
quad.w = clip->w;
quad.h = clip->h;
}
SDL_RenderCopyEx( renderer, this->_texture, clip, &quad, angle, center, flip );
}
}
\ No newline at end of file