Skip to content

Instantly share code, notes, and snippets.

@sponge
Created July 18, 2013 05:21
Show Gist options
  • Save sponge/6026879 to your computer and use it in GitHub Desktop.
Save sponge/6026879 to your computer and use it in GitHub Desktop.
//
// main.h
// HelloSDL
//
// Created by Richard Carter on 5/30/11.
// Copyright 2011 Richard Carter. All rights reserved.
//
static const int tile_size = 16;
static const int window_width = 800;
static const int window_height = 600;
static const char *window_title = "Hello World!";
static bool running = true;
SDL_Window *win;
SDL_Renderer *ren;
SDL_Rect *tiles;
SDL_Texture *sheet;
int SDL_main (int argc, char **argv);
void mainloop();
===============================
#include <stdlib.h>
#include <string>
#include <SDL.h>
#include <SDL_image.h>
#include "main.h"
using namespace std;
SDL_Texture* LoadImage(std::string file) {
SDL_Surface *loadedImage;
SDL_Texture *texture;
loadedImage = IMG_Load(file.c_str());
if (loadedImage){
texture = SDL_CreateTextureFromSurface(ren, loadedImage);
SDL_FreeSurface(loadedImage);
} else {
printf("%s", SDL_GetError());
exit(1);
}
return texture;
}
void ApplySurface(int x, int y, const SDL_Rect *quad, SDL_Renderer *rend) {
const SDL_Rect dstrect = {x, y, tile_size, tile_size};
SDL_RenderCopy(rend, sheet, quad, &dstrect);
}
int SDL_main (int argc, char **argv) {
if (SDL_Init(SDL_INIT_EVERYTHING) == -1) {
return 1;
}
win = SDL_CreateWindow(window_title, 100, 100, window_width, window_height, SDL_WINDOW_SHOWN);
if (!win) {
return 1;
}
ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
if (!ren) {
return 1;
}
mainloop();
SDL_DestroyWindow(win);
SDL_Quit();
return 0;
}
void handle_event(SDL_Event &evt, float dt) {
if (evt.type == SDL_QUIT) {
running = false;
}
}
void update(float dt) {
}
void render() {
uint64_t start = SDL_GetPerformanceCounter();
SDL_RenderClear(ren);
int i = 0;
for ( int y = 0; y < 64; y++ ) {
for (int x = 0; x < 64; x++) {
i = (i+1) % 611;
ApplySurface(x*16, y*16, &tiles[i], ren);
}
}
uint64_t end = SDL_GetPerformanceCounter();
float dt = (end - start) / (float)SDL_GetPerformanceFrequency();
if ( dt >= 8.0f ) {
printf("%0.2f\n", dt);
}
}
void mainloop() {
SDL_Surface *surface = IMG_Load("media/keen1.png");
const int tw = surface->w / tile_size;
const int th = surface->h / tile_size;
tiles = new SDL_Rect[tw*th];
int i = 0;
for ( int y = 0; y < th; y++ ) {
for (int x = 0; x < tw; x++) {
tiles[i++].x = x*tile_size;
tiles[i].y = y*tile_size;
tiles[i].w = tile_size;
tiles[i].h = tile_size;
}
}
sheet = SDL_CreateTextureFromSurface(ren, surface);
SDL_FreeSurface(surface);
SDL_Event evt;
uint64_t old = SDL_GetPerformanceCounter();
while (running) {
uint64_t now = SDL_GetPerformanceCounter();
float dt = (now - old) / (float)SDL_GetPerformanceFrequency();
old = now;
if ( dt >= 16.0f ) {
//printf("%0.2f\n", dt);
}
while (SDL_PollEvent(&evt)) {
handle_event(evt, dt);
}
if (dt > 0.0f) {
update(dt);
}
render();
SDL_RenderPresent(ren);
//SDL_Delay(1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment