Created
January 27, 2016 22:07
-
-
Save johngirvin/fdf60a6e6a9d735b563c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "SDL.h" | |
#include "windows.h" | |
#include <ctime> | |
#include <cstdarg> | |
#include <vector> | |
SDL_DisplayMode mode; | |
SDL_Window *window; | |
SDL_Renderer *renderer; | |
SDL_Surface *image; | |
SDL_Texture *texture; | |
float minX = 0; | |
float maxX = 0; | |
float minY = 0; | |
float maxY = 0; | |
float fpsTime = 0; | |
int fpsCount = 0; | |
WCHAR debug[1024]; | |
typedef struct { | |
float x, xv, y, yv, r, rv; | |
} Bunny; | |
std::vector<Bunny> bunnies; | |
void initBunny(Bunny *bunny); | |
int | |
randomInt(int min, int max) { | |
return min + rand() % (max - min + 1); | |
} | |
void | |
moreBunnies(int more) { | |
Bunny bunny; | |
for (int i = 0; i < more; i++) { | |
initBunny(&bunny); | |
bunnies.push_back(bunny); | |
} | |
} | |
void | |
initBunny(Bunny *bunny) { | |
bunny->x = minX + randomInt((int)minX, (int)maxX); | |
bunny->y = minY + randomInt((int)minY, (int)maxY); | |
do { | |
bunny->xv = 100 - randomInt(0, 200); | |
} while (bunny->xv < 10 && bunny->xv > -10); | |
do { | |
bunny->yv = 100 - randomInt(0, 200); | |
} while (bunny->yv < 10 && bunny->yv > -10); | |
bunny->r = randomInt(0, 360); | |
do { | |
bunny->rv = 50 - randomInt(0, 100); | |
} while (bunny->rv < 10 && bunny->rv > -10); | |
} | |
void | |
renderBunnies(float dt) { | |
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); | |
SDL_RenderClear(renderer); | |
SDL_Rect src = { 0,0,image->w,image->h }; | |
SDL_Rect dst = { 0,0,image->w,image->h }; | |
for (auto bunny = bunnies.begin(); bunny != bunnies.end(); bunny++) { | |
bunny->x += dt * bunny->xv; | |
if (bunny->x < minX) { bunny->x = minX; bunny->xv = -bunny->xv; } | |
if (bunny->x > maxX) { bunny->x = maxX; bunny->xv = -bunny->xv; } | |
bunny->y += dt * bunny->yv; | |
if (bunny->y < minY) { bunny->y = minY; bunny->yv = -bunny->yv; } | |
if (bunny->y > maxY) { bunny->y = maxY; bunny->yv = -bunny->yv; } | |
bunny->r = ((int)round(bunny->r + dt * bunny->rv)) % 360; | |
dst.x = (int)round(bunny->x); | |
dst.y = (int)round(bunny->y); | |
SDL_RenderCopy(renderer, texture, &src, &dst); | |
} | |
SDL_RenderPresent(renderer); | |
} | |
int | |
main(int argc, char *argv[]) { | |
// seed random number generator | |
srand(time(NULL)); | |
// initialize SDL | |
if (SDL_Init(SDL_INIT_VIDEO) < 0) { | |
return 1; | |
} | |
// create window and renderer | |
if (SDL_GetCurrentDisplayMode(0, &mode) != 0) { | |
return 1; | |
} | |
if (SDL_CreateWindowAndRenderer(mode.w, mode.h, 0, &window, &renderer) != 0) { | |
return 1; | |
} | |
// load test image and create texture | |
image = SDL_LoadBMP("wabbit_alpha.bmp"); | |
if (!image) { | |
return 1; | |
} | |
texture = SDL_CreateTextureFromSurface(renderer, image); | |
if (!texture) { | |
return 1; | |
} | |
// initialise bunnies | |
minX = 0; | |
maxX = mode.w - image->w; | |
minY = 0; | |
maxY = mode.h - image->h; | |
moreBunnies(100); | |
// main loop | |
float prevTime = SDL_GetTicks() - (1000.0f / 60.0f); | |
int done = 0; | |
while (!done) { | |
float currTime = SDL_GetTicks(); | |
float deltaTime = (currTime - prevTime) / 1000.0f; | |
prevTime = currTime; | |
SDL_Event event; | |
while (SDL_PollEvent(&event)) { | |
switch (event.type) { | |
case SDL_QUIT: | |
done = 1; | |
break; | |
case SDL_FINGERUP: | |
moreBunnies(100); | |
break; | |
} | |
} | |
renderBunnies(deltaTime); | |
fpsTime += deltaTime; | |
fpsCount++; | |
if (fpsTime > 1.0) { | |
swprintf(debug, 1024, L"%d bunnies %.2f FPS\n", bunnies.size(), (((float)fpsCount) / fpsTime)); | |
OutputDebugString(debug); | |
fpsTime = 0; | |
fpsCount = 0; | |
} | |
} | |
// shutdown | |
SDL_FreeSurface(image); | |
SDL_DestroyWindow(window); | |
SDL_Quit(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment