#include <SDL.h>
#include <SDL_image.h>
#include <SDL_mixer.h>
#include <iostream>
#include "Constants.h"
#include "Texture2D.h"
#include "GameScreenManager.h"
using namespace::std;
//Globals
SDL_Window* gWindow = NULL;
Mix_Music* gMusic = NULL;
GameScreenManager* gameScreenManager;
Uint32 gOldTime;
//Function Prototypes
bool InitSDL();
void CloseSDL();
bool Update();
void Render();
void LoadMusic(string path);
SDL_Renderer* gRenderer = NULL;
//Texture2D* gTexture = NULL;
int main(int argc, char* args[])
{
//Check if SDL was set up correctly
LoadMusic("Music/bubble-bobble.mp3");
if(InitSDL() == true)
{
gameScreenManager = new GameScreenManager(gRenderer, SCREEN_INTRO);
gOldTime = SDL_GetTicks();
bool quit = false;
while(!quit)
{
Render();
quit = Update();
}
}
//Close Window and free resources
CloseSDL();
return 0;
}
bool InitSDL()
{
//Setup SDL
if(SDL_Init(SDL_INIT_VIDEO) < 0)
{
cout << "SDL did not start. Error: " << SDL_GetError();
return false;
}
else
{
//All good, so attempt to create a window
gWindow = SDL_CreateWindow("Games Engine Creation",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH,
SCREEN_HEIGHT,
SDL_WINDOW_SHOWN);
//Did the window get created?
if(gWindow == NULL)
{
//Nope
cout << "Window was not created. Error: " << SDL_GetError();
return false;
}
gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED);
if(gRenderer != NULL)
{
//Intialise PNG loading
int imageFlags = IMG_INIT_PNG;
if(! (IMG_Init(imageFlags) & imageFlags))
{
cout << "SDL_Image could not start. Error: " << IMG_GetError();
return false;
}
/*Load the background texture
gTexture = new Texture2D(gRenderer);
if(!gTexture->LoadFromFile("Images/test.bmp"))
{
return false;
*/
}
else
{
cout << "Renderer could not start. Error: " << SDL_GetError();
return false;
}
return true;
}
if(Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0)
{
cout << "Mixer could not initialise. error: " << Mix_GetError();
return false;
}
}
void CloseSDL()
{
//Clear up texture
//gTexture->Free();
//Destory the game screen manager
delete gameScreenManager;
gameScreenManager = NULL;
//Release the renderer
SDL_DestroyRenderer(gRenderer);
gRenderer = NULL;
//Release the window
SDL_DestroyWindow(gWindow);
gWindow = NULL;
//Quit SDL subsystems
IMG_Quit();
SDL_Quit();
Mix_FreeMusic(gMusic);
gMusic = NULL;
}
bool Update()
{
Uint32 newTime = SDL_GetTicks();
//Event Handler
SDL_Event e;
//Get the events
SDL_PollEvent(&e);
//Handle any events
switch(e.type)
{
case SDL_QUIT:
return true;
break;
}
gameScreenManager->Update((float)(newTime-gOldTime)/1000.0f, e);
gOldTime = newTime;
switch (e.type)
{
case SDL_KEYDOWN:
switch (e.key.keysym.sym)
{
case SDLK_RETURN:
gameScreenManager = new GameScreenManager(gRenderer, SCREEN_LEVEL1);
break;
case SDLK_LSHIFT:
gameScreenManager = new GameScreenManager(gRenderer, SCREEN_LEVEL2);
break;
}
break;
}
if(Mix_PlayingMusic() == 0)
{
Mix_PlayMusic(gMusic, -1);
}
return false;
}
void Render()
{
//Clear the screen
SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
SDL_RenderClear(gRenderer);
gameScreenManager->Render();
//Update Scrren
SDL_RenderPresent(gRenderer);
}
void LoadMusic(string path)
{
gMusic = Mix_LoadMUS(path.c_str());
if(gMusic == NULL)
{
cout << "Failed to load background music! Error: " << Mix_GetError() << endl;
}
}
#include <SDL_image.h>
#include <SDL_mixer.h>
#include <iostream>
#include "Constants.h"
#include "Texture2D.h"
#include "GameScreenManager.h"
using namespace::std;
//Globals
SDL_Window* gWindow = NULL;
Mix_Music* gMusic = NULL;
GameScreenManager* gameScreenManager;
Uint32 gOldTime;
//Function Prototypes
bool InitSDL();
void CloseSDL();
bool Update();
void Render();
void LoadMusic(string path);
SDL_Renderer* gRenderer = NULL;
//Texture2D* gTexture = NULL;
int main(int argc, char* args[])
{
//Check if SDL was set up correctly
LoadMusic("Music/bubble-bobble.mp3");
if(InitSDL() == true)
{
gameScreenManager = new GameScreenManager(gRenderer, SCREEN_INTRO);
gOldTime = SDL_GetTicks();
bool quit = false;
while(!quit)
{
Render();
quit = Update();
}
}
//Close Window and free resources
CloseSDL();
return 0;
}
bool InitSDL()
{
//Setup SDL
if(SDL_Init(SDL_INIT_VIDEO) < 0)
{
cout << "SDL did not start. Error: " << SDL_GetError();
return false;
}
else
{
//All good, so attempt to create a window
gWindow = SDL_CreateWindow("Games Engine Creation",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH,
SCREEN_HEIGHT,
SDL_WINDOW_SHOWN);
//Did the window get created?
if(gWindow == NULL)
{
//Nope
cout << "Window was not created. Error: " << SDL_GetError();
return false;
}
gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED);
if(gRenderer != NULL)
{
//Intialise PNG loading
int imageFlags = IMG_INIT_PNG;
if(! (IMG_Init(imageFlags) & imageFlags))
{
cout << "SDL_Image could not start. Error: " << IMG_GetError();
return false;
}
/*Load the background texture
gTexture = new Texture2D(gRenderer);
if(!gTexture->LoadFromFile("Images/test.bmp"))
{
return false;
*/
}
else
{
cout << "Renderer could not start. Error: " << SDL_GetError();
return false;
}
return true;
}
if(Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0)
{
cout << "Mixer could not initialise. error: " << Mix_GetError();
return false;
}
}
void CloseSDL()
{
//Clear up texture
//gTexture->Free();
//Destory the game screen manager
delete gameScreenManager;
gameScreenManager = NULL;
//Release the renderer
SDL_DestroyRenderer(gRenderer);
gRenderer = NULL;
//Release the window
SDL_DestroyWindow(gWindow);
gWindow = NULL;
//Quit SDL subsystems
IMG_Quit();
SDL_Quit();
Mix_FreeMusic(gMusic);
gMusic = NULL;
}
bool Update()
{
Uint32 newTime = SDL_GetTicks();
//Event Handler
SDL_Event e;
//Get the events
SDL_PollEvent(&e);
//Handle any events
switch(e.type)
{
case SDL_QUIT:
return true;
break;
}
gameScreenManager->Update((float)(newTime-gOldTime)/1000.0f, e);
gOldTime = newTime;
switch (e.type)
{
case SDL_KEYDOWN:
switch (e.key.keysym.sym)
{
case SDLK_RETURN:
gameScreenManager = new GameScreenManager(gRenderer, SCREEN_LEVEL1);
break;
case SDLK_LSHIFT:
gameScreenManager = new GameScreenManager(gRenderer, SCREEN_LEVEL2);
break;
}
break;
}
if(Mix_PlayingMusic() == 0)
{
Mix_PlayMusic(gMusic, -1);
}
return false;
}
void Render()
{
//Clear the screen
SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
SDL_RenderClear(gRenderer);
gameScreenManager->Render();
//Update Scrren
SDL_RenderPresent(gRenderer);
}
void LoadMusic(string path)
{
gMusic = Mix_LoadMUS(path.c_str());
if(gMusic == NULL)
{
cout << "Failed to load background music! Error: " << Mix_GetError() << endl;
}
}