I would like a little help with the implementation of my SDL2 engine. It's actually not a real engine nor does it strive to be but I don't have a better word to describe it.
The purpose of this project is to serve as a base for other things to be built on, like a visualization of fractals for example, so i would like to make it as optimal as possible.
For now it just creates a window, renderer and handles basic input handling. Before I move on with it I would like to iron out any kinks there might be so any improvement suggestion would come in handy, especially when it comes to performance and future upgradability.
engine.h
#ifndef ENGINE_H
#define ENGINE_H
#pragma once
#include <SDL2/SDL.h>
#include "window.hpp"
#include "renderer.hpp"
class Engine
{
private:
bool running;
public:
Window window;
Renderer renderer;
Engine();
~Engine();
//Handles input
void handleInput();
//draws things onto the screen
void draw();
bool isRunning();
};
#endif
engine.cpp
#include "engine.h"
Engine::Engine(): window( 640, 480 ), renderer( window.getWindow() )
{
if( window.getWindow() == NULL )
{
printf("Window could not be created. \n" );
running = false;
}
else
{
if( renderer.getRenderer() == NULL )
{
printf("Renderer could not be created. \n" );
running = false;
}
running = true;
printf("Engine has been started successfuly \n" );
}
}
Engine::~Engine()
{
running = false;
window.close();
renderer.close();
}
void Engine::handleInput()
{
SDL_Event event;
while( SDL_PollEvent( &event ) != 0 )
{
if( event.type == SDL_QUIT )
{
window.close();
}
}
}
void Engine::draw()
{
SDL_RenderClear( renderer.getRenderer() );
SDL_RenderPresent( renderer.getRenderer() );
}
bool Engine::isRunning()
{
return window.isOpen() && renderer.isCreated() && running;
}
window.hpp
#ifndef WIDNOW_H
#define WINDOW_H
#pragma once
#include <SDL2/SDL.h>
class Window
{
private:
const int SCREEN_WIDTH = 0;
const int SCREEN_HEIGHT = 0;
bool open = false;
//Window we'll render to
SDL_Window* mWindow = NULL;
public:
Window();
//Consturctor to initialize window
Window( int SCREEN_WIDTH, int SCREEN_HEIGHT );
~Window();
void close();
bool isOpen();
SDL_Window* getWindow();
};
#endif
window.cpp
#include "window.hpp"
Window::Window()
{
}
//Initialize SDL and create a window
Window::Window( int SCREEN_HEIGHT, int SCREEN_WIDTH ): SCREEN_WIDTH(SCREEN_WIDTH), SCREEN_HEIGHT( SCREEN_HEIGHT)
{
//If SDL video was not initialize
if( SDL_Init( SDL_INIT_VIDEO) < 0 )
{
printf( "SDL could not initialize! SDL error: %s\n", SDL_GetError() );
open = false;
}
else
{
//Create window
mWindow = SDL_CreateWindow( "Window class", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
//If the window was not created
if( mWindow == NULL )
{
printf( "Could not create window. SDL error: %s\n", SDL_GetError() );
open = false;
}
//Set the open flag to true after the operation completed successfuly
open = true;
printf("Window was created successfuly.\n" );
}
}
void Window::close()
{
open = false;
SDL_DestroyWindow( mWindow );
mWindow = NULL;
SDL_Quit();
}
Window::~Window()
{
printf("Window destructor was called \n" );
close();
}
bool Window::isOpen()
{
return open;
}
SDL_Window* Window::getWindow()
{
return mWindow;
}
renderer.hpp
#ifndef RENDERER_H
#define RENDERER_H
#pragma once
#include "window.hpp"
class Renderer : public Window
{
private:
bool created = false;
SDL_Renderer* mRenderer = NULL;
public:
Renderer();
Renderer( SDL_Window* window );
~Renderer();
void close();
bool isCreated();
SDL_Renderer* getRenderer();
};
#endif
renderer.cpp
#include "renderer.hpp"
Renderer::Renderer()
{
}
Renderer::Renderer( SDL_Window* window )
{
//create the renderer
mRenderer = SDL_CreateRenderer( window, -1, SDL_RENDERER_ACCELERATED );
//if the rendere was not created
if( mRenderer == NULL )
{
printf("Renderer could not be created. SDL error: %s\n", SDL_GetError() );
created = false;
}
else
{
//Set the render draw color to white
SDL_SetRenderDrawColor( mRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
created = true;
printf("Renderer was created successfuly \n" );
}
}
Renderer::~Renderer()
{
printf("Renderer desturctor was called \n" );
close();
}
bool Renderer::isCreated()
{
return created;
}
void Renderer::close()
{
SDL_DestroyRenderer( mRenderer );
mRenderer = NULL;
}
SDL_Renderer* Renderer::getRenderer()
{
return mRenderer;
}
main.cpp
#include "engine.h"
int main( int argc, char* argv[] )
{
Engine engine;
while( engine.isRunning() )
{
engine.handleInput();
engine.draw();
}
}