CS116b/CS216
Chris Pollett
Feb 19, 2014
Let's first create a simple app that does nothing, but does compile and execute. It will have the basic functions for a larger project. First, let's create the following header file MySdlApplication.h and add it to our project.
#ifndef _MYSDLAPPLICATION_H_
#define _MYSDLAPPLICATION_H_
#include <iostream>
#include <SDL2/SDL.h>
//Windows gets confused on main the entry point because of SDL, so ...
#ifdef __WIN32__
#undef main
#endif
class MySdlApplication
{
private:
bool running;
public:
MySdlApplication();
int onExecute();
bool onInit();
void onEvent(SDL_Event* event);
void onLoop();
void onRender();
void onCleanup();
};
#endif
/**
MySdlApplication
Created by Chris Pollett on 2/19/14.
*/
#include "MySdlApplication.h"
MySdlApplication::MySdlApplication() {
running = true;
}
bool MySdlApplication::onInit() {
return true;
}
void MySdlApplication::onEvent(SDL_Event* event) {
}
void MySdlApplication::onLoop() {
}
void MySdlApplication::onRender() {
}
void MySdlApplication::onCleanup() {
}
int MySdlApplication::onExecute() {
if(onInit() == false) {
return -1;
}
SDL_Event Event;
while(running) {
while(SDL_PollEvent(&event)) {
onEvent(&event);
}
onLoop();
onRender();
}
onCleanup();
return 0;
}
int main(int argc, const char * argv[])
{
MySdlApplication application;
std::cout << "Hello, World!\n";
return application.onExecute();
}
bool MySdlApplication::onInit() {
if(SDL_Init(SDL_INIT_EVERYTHING) < 0) {
return false;
}
if((display = SDL_CreateWindow("My SDL Application",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, 640, 480,
SDL_WINDOW_OPENGL)) == NULL) {
return false;
}
return true;
}
void MySdlApplication::onEvent(SDL_Event* event) {
if(event->type == SDL_QUIT) {
running = false;
}
}
typedef union{
Uint8 type;
SDL_ActiveEvent active;
SDL_KeyboardEvent key;
SDL_MouseMotionEvent motion;
SDL_MouseButtonEvent button;
SDL_JoyAxisEvent jaxis;
SDL_JoyBallEvent jball;
SDL_JoyHatEvent jhat;
SDL_JoyButtonEvent jbutton;
SDL_ResizeEvent resize;
SDL_ExposeEvent expose;
SDL_QuitEvent quit;
SDL_UserEvent user;
SDL_SysWMEvent syswm;
} SDL_Event;