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 (2)
File added
assets/arrow.png

1.44 KiB

#ifndef __COLOUR_H__
#define __COLOUR_H__
#include <cstdint>;
#include <cstdint>
struct RGBA{
uint8_t r;
......
#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
......@@ -8,11 +8,10 @@
#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:
......@@ -20,8 +19,14 @@ class cTexture : public CComponentGeneric
~cTexture();
bool loadFromFile( std::string path );
void free();
void render( Vec2 pt );
void render( Vec2 pt, SDL_Renderer *renderer, SDL_Rect *clip = NULL );
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; }
......@@ -31,10 +36,13 @@ class cTexture : public CComponentGeneric
void setBlendMode( SDL_BlendMode blending );
void setAlpha( uint8_t alpha );
private:
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-generic.h"
#include "maths/maths.h"
#include "components/cTexture.h"
class eButton : public EntityGeneric
{
public:
void setPosition( Vec2 pos );
void handleEvent( SDL_Event* evt );
void render();
private:
Vec2 _pos;
cTexture _sprite;
};
#endif // __ENTITIES__BUTTON
......@@ -12,8 +12,13 @@
#include "logger.h"
#include "colour.h"
#include "maths/vec2.h"
#include "components/cTexture.h"
// #include "components/cFontfile.h"
// #include "components/cTexture.h"
class cFontfile;
class cTexture;
const uint16_t SCREEN_HEIGHT = 600;
......@@ -56,12 +61,15 @@ class Window
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;
......@@ -86,4 +94,5 @@ class Window
void clean();
};
#endif // __WINDOW_H__
#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
......@@ -2,6 +2,7 @@
cTexture::cTexture()
: _texture( NULL ),
_logger( Logger::instance() ),
_width( 0 ), _height( 0 )
{}
......@@ -50,7 +51,7 @@ void cTexture::setColour( uint8_t red, uint8_t green, uint8_t blue ) { SDL_SetTe
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( Vec2 pt, SDL_Renderer *renderer, SDL_Rect* clip )
void cTexture::render( SDL_Renderer *renderer, Vec2 pt, SDL_Rect* clip, double angle, SDL_Point* center, SDL_RendererFlip flip )
{
if( this->_width && this->_height )
{
......@@ -60,14 +61,6 @@ void cTexture::render( Vec2 pt, SDL_Renderer *renderer, SDL_Rect* clip )
quad.w = clip->w;
quad.h = clip->h;
}
SDL_RenderCopy( renderer, this->_texture, clip, &quad );
}
}
void cTexture::render( Vec2 pt )
{
if( Window::renderer != NULL )
{
SDL_Rect quad = { (int)pt.x, (int)pt.y, this->_width, this->_height };
SDL_RenderCopy( Window::renderer, this->_texture, NULL, &quad );
SDL_RenderCopyEx( renderer, this->_texture, clip, &quad, angle, center, flip );
}
}
\ No newline at end of file
#include "entities/eButton.h"
void eButton::setPosition( Vec2 pos ){}
void eButton::handleEvent( SDL_Event* evt ){}
void eButton::render(){}
\ No newline at end of file
......@@ -36,8 +36,8 @@ void GameEngine::run()
this->init();
this->isRunning = true;
this->_frame = 0;
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
std::time_t _frametime = std::chrono::system_clock::to_time_t(now);
std::chrono::system_clock::time_point now = std::chrono::high_resolution_clock::now();
std::time_t _frametime = std::chrono::high_resolution_clock::to_time_t(now);
while( this->isRunning ) { this->update(); }
}
......@@ -47,13 +47,15 @@ void GameEngine::update()
this->handleInput();
this->_entityManager.update();
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
std::chrono::system_clock::time_point now = std::chrono::high_resolution_clock::now();
if ( ++this->_frame % 1 == 0 ) {
std::time_t cTime = std::chrono::system_clock::to_time_t(now);
std::time_t cTime = std::chrono::high_resolution_clock::to_time_t(now);
size_t delTime = cTime - this->_frametime;
this->_frametime = cTime;
printf( "Frame: %i at %.2f fps\n", this->_frame, delTime == 0 ? 0.0 : 1.0 / delTime );
std::cout << cTime << std::endl;
// printf( "Frame: %i at %.2f fps\n", this->_frame, delTime == 0 ? 0.0 : 1.0 / delTime );
printf( "Frame: %i at %.2f fps over %i s\n", this->_frame, delTime == 0 ? 0.0 : 1.0 / delTime, delTime );
}
this->_window->update();
}
......@@ -70,22 +72,6 @@ void GameEngine::handleInput()
{
case SDL_QUIT: this->close(); break;
case SDLK_ESCAPE: this->close(); break;
// case SDLK_PAGEUP:
// this->_screenModulation.brighten( 5 );
// break;
// case SDLK_PAGEDOWN:
// this->_screenModulation.darken( 5 );
// break;
// case SDLK_PLUS:
// case SDLK_KP_PLUS:
// case SDLK_GREATER:
// if( this->_alpha < 0xFF ) this->_alpha += 5;
// break;
// case SDLK_LESS:
// case SDLK_MINUS:
// case SDLK_KP_MINUS:
// if( this->_alpha > 5 ) this->_alpha -= 5;
// break;
}
}
}
......
#include "window.h"
#include "components/cFontfile.h"
#include "components/cTexture.h"
bool Window::sdlIsInit = false;
SDL_Renderer* Window::renderer = NULL;
......@@ -14,7 +16,9 @@ Window::Window()
this->_renderSurface = nullptr;
this->_background = new cTexture();
this->_character = new cTexture();
this->_arrow = new cTexture();
this->_water = new cTexture();
this->_fontFile = new cFontfile();
this->_textureSprite = new cTexture();
this->_screenModulation = Colour( 0x88, 0x88, 0x88, 0xFF );
......@@ -38,6 +42,7 @@ void Window::initialise()
this->_toClose = false;
this->_hasInit = true;
this->_frame = 0;
}
SDL_Texture* Window::loadTexture( std::string path )
......@@ -85,6 +90,9 @@ void Window::loadMedia()
{
this->_background->loadFromFile( "assets/background.png" );
this->_character->loadFromFile( "assets/person.png" );
this->_arrow->loadFromFile( "assets/arrow.png" );
this->_fontFile->loadFromFile( "assets/MonoRegular-jEmAR.ttf" );
this->_fontFile->setColour( 255, 255, 0 );
if( this->_water->loadFromFile( "assets/water_animated.png" ) )
{
// Set Frame 1
......@@ -156,20 +164,19 @@ void Window::update() {
SDL_Rect bg = this->_createViewport( 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT );
this->_background->setColour( this->_screenModulation );
this->_background->render( {0,0}, this->_renderer );
this->_character->render( {SCREEN_HEIGHT/3,SCREEN_HEIGHT/3}, this->_renderer );
// this->_textureSprite->render( {0,0}, this->_renderer );
this->_background->render( this->_renderer, {0,0} );
this->_character->render( this->_renderer, {SCREEN_HEIGHT/3,SCREEN_HEIGHT/3} );
// this->_textureSprite->render( this->_renderer, {0,0} );
//Render top left sprite
this->_textureSprite->setAlpha( this->_alpha );
this->_textureSprite->render( {0, 0}, this->_renderer, &this->_sprites[ 0 ] );
this->_textureSprite->render( this->_renderer, {0, 0}, &this->_sprites[ 0 ] );
//Render top right sprite
this->_textureSprite->render( {SCREEN_WIDTH - this->_sprites[ 1 ].w, 0}, this->_renderer, &this->_sprites[ 1 ] );
this->_textureSprite->render( this->_renderer, {SCREEN_WIDTH - this->_sprites[ 1 ].w, 0}, &this->_sprites[ 1 ] );
//Render bottom left sprite
this->_textureSprite->render( {0, SCREEN_HEIGHT - this->_sprites[ 2 ].h}, this->_renderer, &this->_sprites[ 2 ] );
this->_textureSprite->render( this->_renderer, {0, SCREEN_HEIGHT - this->_sprites[ 2 ].h}, &this->_sprites[ 2 ] );
//Render bottom right sprite
this->_textureSprite->render( {SCREEN_WIDTH - this->_sprites[ 3 ].w, SCREEN_HEIGHT - this->_sprites[ 3 ].h}, this->_renderer, &this->_sprites[ 3 ] );
this->_textureSprite->render( this->_renderer, {SCREEN_WIDTH - this->_sprites[ 3 ].w, SCREEN_HEIGHT - this->_sprites[ 3 ].h}, &this->_sprites[ 3 ] );
SDL_Rect core = this->_createViewport( SCREEN_WIDTH / 4, SCREEN_HEIGHT / 4, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 );
......@@ -183,7 +190,13 @@ void Window::update() {
this->_drawPoint( { SCREEN_WIDTH / 2, i } );
}
// Water
this->_water->render( this->_renderer, { 0, 0 }, &this->_waterTicks[ this->_frame % this->_waterFrames ] );
SDL_Rect overlay = this->_createViewport( 0, 0, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 );
this->_arrow->render( this->_renderer, {100, 100}, NULL, this->_frame % 360 );
const int hexRad = 10;
const int hexSpacing = 1;
const int drawHexRad = hexRad + hexSpacing;
......@@ -202,7 +215,11 @@ void Window::update() {
// SDL_RenderCopy( this->_renderer, this->_activeTexture, NULL, NULL );
overlay = this->_createViewport( 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT );
this->_fontFile->renderText( std::to_string(this->_frame) );
this->_fontFile->render( this->_renderer, {50,50} );
++this->_frame;
SDL_RenderPresent( this->_renderer );
}
......@@ -296,36 +313,6 @@ void Window::spawn()
void Window::processInput()
{
SDL_Event e;
while( SDL_PollEvent( &e ) )
{
// std::cout << "event" << std::endl;
if( e.type == SDL_QUIT ) this->_toClose = true;
else if( e.type == SDL_KEYDOWN )
{
switch( e.key.keysym.sym )
{
case SDL_QUIT: this->_toClose = true; break;
case SDLK_ESCAPE: this->_toClose = true; break;
case SDLK_PAGEUP:
this->_screenModulation.brighten( 5 );
break;
case SDLK_PAGEDOWN:
this->_screenModulation.darken( 5 );
break;
case SDLK_PLUS:
case SDLK_KP_PLUS:
case SDLK_GREATER:
if( this->_alpha < 0xFF ) this->_alpha += 5;
break;
case SDLK_LESS:
case SDLK_MINUS:
case SDLK_KP_MINUS:
if( this->_alpha > 5 ) this->_alpha -= 5;
break;
}
}
}
}
void Window::clean()
......