Skip to content
Snippets Groups Projects
Commit 3ab8cca2 authored by Alfred Burgess's avatar Alfred Burgess
Browse files

Implemented basic window image drawing

parent c666d593
No related merge requests found
assets/hello-world.bmp

91.2 KiB

assets/sq.bmp

7.36 KiB

...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
#define SDL_MAIN_HANDLED #define SDL_MAIN_HANDLED
#include <SDL.h> #include <SDL.h>
#include <cstdio> #include <cstdio>
#include <cstring>
#include <string>
#include "logger.h" #include "logger.h"
...@@ -17,13 +19,20 @@ class Window ...@@ -17,13 +19,20 @@ class Window
static bool sdlIsInit; static bool sdlIsInit;
Window(); Window();
~Window(); ~Window();
void loadMedia( std::string path );
void update();
void processInput();
bool isClosed() { return this->_toClose; }
void spawn(); void spawn();
private: private:
bool _toClose; bool _toClose;
bool _hasInit; bool _hasInit;
Logger& _logger; Logger& _logger;
SDL_Window* _window; SDL_Window* _window;
SDL_Surface* _surface; SDL_Surface* _screenSurface;
SDL_Surface* _renderSurface;
void initialise(); void initialise();
void clean(); void clean();
......
...@@ -39,12 +39,15 @@ int main( int argc, char* argv[] ) { ...@@ -39,12 +39,15 @@ int main( int argc, char* argv[] ) {
// Initialise Window // Initialise Window
Window* window = new Window(); Window* window = new Window();
window->loadMedia( "assets/hello-world.bmp" );
window->update();
window->spawn(); window->spawn();
// Main Loop // Main Loop
while( state != Exit ) while( state != Exit )
{ {
if( ++tickCount > 1000000 ) // if( ++tickCount > 1000000 )
if( window->isClosed() )
{ {
// Exit // Exit
state = Exit; state = Exit;
...@@ -53,10 +56,12 @@ int main( int argc, char* argv[] ) { ...@@ -53,10 +56,12 @@ int main( int argc, char* argv[] ) {
// Update Entities // Update Entities
entityManager.update(); entityManager.update();
// Process Input // Process Input
window->processInput();
// Movement Updates // Movement Updates
// Collision Updates // Collision Updates
// Update Game State // Update Game State
// Render Game // Render Game
window->update();
_frame++; _frame++;
} }
......
...@@ -6,6 +6,9 @@ Window::Window() ...@@ -6,6 +6,9 @@ Window::Window()
: _logger( Logger::instance() ), _hasInit( false ) : _logger( Logger::instance() ), _hasInit( false )
{ {
this->initialise(); this->initialise();
this->_window = nullptr;
this->_screenSurface = nullptr;
this->_renderSurface = nullptr;
} }
Window::~Window() Window::~Window()
{} {}
...@@ -26,6 +29,25 @@ void Window::initialise() ...@@ -26,6 +29,25 @@ void Window::initialise()
this->_hasInit = true; this->_hasInit = true;
} }
void Window::loadMedia( std::string path )
{
//Load splash image
this->_renderSurface = SDL_LoadBMP( path.c_str() );
if( this->_renderSurface == NULL )
{
this->_logger.error( "Unable to load image" );
} else {
this->_logger.info( "Loaded image media" );
SDL_BlitSurface( this->_renderSurface, NULL, this->_screenSurface, NULL );
}
}
void Window::update() {
SDL_BlitSurface( this->_renderSurface, NULL, this->_screenSurface, NULL );
SDL_UpdateWindowSurface( this->_window );
}
void Window::spawn() void Window::spawn()
{ {
this->_window = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN ); this->_window = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
...@@ -34,33 +56,40 @@ void Window::spawn() ...@@ -34,33 +56,40 @@ void Window::spawn()
printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() ); printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
} else { } else {
//Get window surface //Get window surface
this->_surface = SDL_GetWindowSurface( this->_window ); this->_screenSurface = SDL_GetWindowSurface( this->_window );
//Fill the surface white //Fill the surface white
SDL_FillRect( this->_surface, NULL, SDL_MapRGB( this->_surface->format, 0xFF, 0xFF, 0xFF ) ); SDL_FillRect( this->_screenSurface, NULL, SDL_MapRGB( this->_screenSurface->format, 0xFF, 0x00, 0xFF ) );
//Update the surface //Update the surface
SDL_UpdateWindowSurface( this->_window ); SDL_UpdateWindowSurface( this->_window );
}
}
//Hack to get window to stay up void Window::processInput()
SDL_Event e; {
while( ! this->_toClose ) SDL_Event e;
{ while( SDL_PollEvent( &e ) )
while( SDL_PollEvent( &e ) ) {
{ if( e.type == SDL_QUIT ) this->_toClose = true; } // std::cout << "event" << std::endl;
} if( e.type == SDL_QUIT ) this->_toClose = true;
} }
} }
void Window::clean() void Window::clean()
{ {
// Clean Surfaces
SDL_FreeSurface( this->_screenSurface );
SDL_FreeSurface( this->_renderSurface );
this->_screenSurface = nullptr;
this->_renderSurface = nullptr;
//Destroy window //Destroy window
SDL_DestroyWindow( this->_window ); SDL_DestroyWindow( this->_window );
this->_window = nullptr;
//Quit SDL subsystems //Quit SDL subsystems
SDL_Quit(); SDL_Quit();
// delete this->_window;
// delete this->_surface;
this->_logger.info( "Window Closed and clean" ); this->_logger.info( "Window Closed and clean" );
} }
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment