#include "SDL.h"
#include "common.h"
#define NUM_HAPPY_FACES 100
#define HAPPY_FACE_SIZE 32
static SDL_Texture *texture = 0;
static struct
{
float x, y;
float xvel, yvel;
} faces[NUM_HAPPY_FACES];
void
initializeHappyFaces(SDL_Renderer *renderer)
{
int i;
int w;
int h;
SDL_RenderGetLogicalSize(renderer, &w, &h);
for (i = 0; i < NUM_HAPPY_FACES; i++) {
faces[i].x = randomFloat(0.0f, w - HAPPY_FACE_SIZE);
faces[i].y = randomFloat(0.0f, h - HAPPY_FACE_SIZE);
faces[i].xvel = randomFloat(-60.0f, 60.0f);
faces[i].yvel = randomFloat(-60.0f, 60.0f);
}
}
void
render(SDL_Renderer *renderer, double deltaTime)
{
int i;
SDL_Rect srcRect;
SDL_Rect dstRect;
int w;
int h;
SDL_RenderGetLogicalSize(renderer, &w, &h);
int maxx = w - HAPPY_FACE_SIZE;
int maxy = h - HAPPY_FACE_SIZE;
int minx = 0;
int miny = 0;
srcRect.x = 0;
srcRect.y = 0;
srcRect.w = HAPPY_FACE_SIZE;
srcRect.h = HAPPY_FACE_SIZE;
dstRect.w = HAPPY_FACE_SIZE;
dstRect.h = HAPPY_FACE_SIZE;
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
for (i = 0; i < NUM_HAPPY_FACES; i++) {
faces[i].x += faces[i].xvel * deltaTime;
faces[i].y += faces[i].yvel * deltaTime;
if (faces[i].x > maxx) {
faces[i].x = maxx;
faces[i].xvel = -faces[i].xvel;
} else if (faces[i].y > maxy) {
faces[i].y = maxy;
faces[i].yvel = -faces[i].yvel;
}
if (faces[i].x < minx) {
faces[i].x = minx;
faces[i].xvel = -faces[i].xvel;
} else if (faces[i].y < miny) {
faces[i].y = miny;
faces[i].yvel = -faces[i].yvel;
}
dstRect.x = faces[i].x;
dstRect.y = faces[i].y;
SDL_RenderCopy(renderer, texture, &srcRect, &dstRect);
}
SDL_RenderPresent(renderer);
}
void
initializeTexture(SDL_Renderer *renderer)
{
SDL_Surface *bmp_surface;
bmp_surface = SDL_LoadBMP("icon.bmp");
if (bmp_surface == NULL) {
fatalError("could not load bmp");
}
SDL_SetColorKey(bmp_surface, 1,
SDL_MapRGB(bmp_surface->format, 255, 255, 255));
texture = SDL_CreateTextureFromSurface(renderer, bmp_surface);
if (!texture) {
fatalError("could not create texture");
}
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
SDL_FreeSurface(bmp_surface);
}
int
main(int argc, char *argv[])
{
SDL_Window *window;
SDL_Renderer *renderer;
int done;
int width;
int height;
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
fatalError("Could not initialize SDL");
}
window = SDL_CreateWindow(NULL, 0, 0, 320, 480, SDL_WINDOW_FULLSCREEN | SDL_WINDOW_ALLOW_HIGHDPI);
renderer = SDL_CreateRenderer(window, -1, 0);
SDL_GetWindowSize(window, &width, &height);
SDL_RenderSetLogicalSize(renderer, width, height);
initializeTexture(renderer);
initializeHappyFaces(renderer);
done = 0;
while (!done) {
SDL_Event event;
double deltaTime = updateDeltaTime();
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
done = 1;
}
}
render(renderer, deltaTime);
SDL_Delay(1);
}
SDL_DestroyTexture(texture);
SDL_Quit();
return 0;
}