SDL




CS116b/CS216

Chris Pollett

Feb 19, 2014

Outline

Introduction

SDL

Setting up an SDL Environment

First App -- Header File

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

First App -- Implementation File

/**
   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();
}

Remarks

Making a Simple Window, Responding to a Close Window Event

Events