Skip to content

Instantly share code, notes, and snippets.

@iamricard
Created March 30, 2014 21:04
Show Gist options
  • Save iamricard/9879750 to your computer and use it in GitHub Desktop.
Save iamricard/9879750 to your computer and use it in GitHub Desktop.
sdl2, box2d, cmake
// Copyright 2014-present [Ricard Sole <@rcsole, [email protected]>]
#include <cstdio>
#include "./Game.h"
bool Game::init(const char* title, int xpos, int ypos, int width,
int height, int flags) {
if (SDL_Init(SDL_INIT_EVERYTHING) == 0) {
printf("SDL_Init success\n");
win = SDL_CreateWindow(title,
xpos, ypos,
width, height,
flags);
if (win != 0) {
printf("SDL_CreateWindow success\n");
renderer = SDL_CreateRenderer(win, -1, 0);
if (renderer != 0) {
printf("SDL_CreateRenderer success\n");
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
} else {
printf("SDL_CreateRenderer error\n");
return false;
}
} else {
printf("SDL_CreateWindow error\n");
return false;
}
} else {
printf("SDL_Init error\n");
return false;
}
printf("Init succes\n");
m_bRunning = true;
return true;
}
void Game::render() {
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
}
void Game::clean() {
printf("cleaning game\n");
SDL_DestroyWindow(win);
SDL_DestroyRenderer(renderer);
SDL_Quit();
}
void Game::handleEvents() {
SDL_Event event;
if (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
m_bRunning = false;
break;
default:
break;
}
}
}
// Copyright 2014-present [Ricard Sole <@rcsole, [email protected]>]
#ifndef __Game__
#define __Game__
#include <SDL2/SDL.h>
class Game {
public:
Game() {}
~Game() {}
bool init(const char* title,
int xpos, int ypos,
int width, int height,
int flags);
void render();
void update();
void handleEvents();
void clean();
bool running() { return m_bRunning; }
private:
SDL_Window* m_pWindow;
SDL_Renderer* m_pRenderer;
bool m_bRunning;
};
#endif
// Copyright 2014-present [Ricard Sole <@rcsole, [email protected]>]
#include <cstdio>
#include "Game/Game.h"
Game* game = 0;
int main(int argc, char const *argv[]) {
game = new Game();
game->init("Game Engine v0.0.1",
100, 100,
640, 480,
SDL_WINDOW_RESIZABLE);
while (game->running()) {
game->handleEvents();
// game->update();
game->render();
}
// printf("OK Bye");
game->clean();
return 0;
}
# Author: Mr Foo Bar
# Date: 30 March 2055
# CMake minimum version
cmake_minimum_required(VERSION 2.8)
# Project Name
project(myproject)
# Add ./src directory
add_subdirectory(src)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment