SDL  2.0
happy.c File Reference
#include "SDL.h"
#include "common.h"
+ Include dependency graph for happy.c:

Go to the source code of this file.

Macros

#define NUM_HAPPY_FACES   100 /* number of faces to draw */
 
#define HAPPY_FACE_SIZE   32 /* width and height of happyface */
 

Functions

void initializeHappyFaces (SDL_Renderer *renderer)
 
void render (SDL_Renderer *renderer, double deltaTime)
 
void initializeTexture (SDL_Renderer *renderer)
 
int main (int argc, char *argv[])
 

Variables

static SDL_Texturetexture = 0
 
struct {
   float   x
 
   float   y
 
   float   xvel
 
   float   yvel
 
faces [NUM_HAPPY_FACES]
 

Macro Definition Documentation

◆ HAPPY_FACE_SIZE

#define HAPPY_FACE_SIZE   32 /* width and height of happyface */

Definition at line 11 of file happy.c.

Referenced by initializeHappyFaces(), and render().

◆ NUM_HAPPY_FACES

#define NUM_HAPPY_FACES   100 /* number of faces to draw */

Definition at line 10 of file happy.c.

Referenced by initializeHappyFaces(), and render().

Function Documentation

◆ initializeHappyFaces()

void initializeHappyFaces ( SDL_Renderer renderer)

Definition at line 26 of file happy.c.

References faces, HAPPY_FACE_SIZE, i, NUM_HAPPY_FACES, randomFloat(), and SDL_RenderGetLogicalSize.

Referenced by main().

27 {
28  int i;
29  int w;
30  int h;
31  SDL_RenderGetLogicalSize(renderer, &w, &h);
32 
33  for (i = 0; i < NUM_HAPPY_FACES; i++) {
34  faces[i].x = randomFloat(0.0f, w - HAPPY_FACE_SIZE);
35  faces[i].y = randomFloat(0.0f, h - HAPPY_FACE_SIZE);
36  faces[i].xvel = randomFloat(-60.0f, 60.0f);
37  faces[i].yvel = randomFloat(-60.0f, 60.0f);
38  }
39 }
#define HAPPY_FACE_SIZE
Definition: happy.c:11
GLfloat GLfloat GLfloat GLfloat h
GLfloat f
float randomFloat(float min, float max)
Definition: common.c:26
GLubyte GLubyte GLubyte GLubyte w
static struct @62 faces[NUM_HAPPY_FACES]
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
#define NUM_HAPPY_FACES
Definition: happy.c:10
#define SDL_RenderGetLogicalSize

◆ initializeTexture()

void initializeTexture ( SDL_Renderer renderer)

Definition at line 106 of file happy.c.

References fatalError(), SDL_Surface::format, NULL, SDL_BLENDMODE_BLEND, SDL_CreateTextureFromSurface, SDL_FreeSurface, SDL_LoadBMP, SDL_MapRGB, SDL_SetColorKey, and SDL_SetTextureBlendMode.

Referenced by main().

107 {
108  SDL_Surface *bmp_surface;
109  /* load the bmp */
110  bmp_surface = SDL_LoadBMP("icon.bmp");
111  if (bmp_surface == NULL) {
112  fatalError("could not load bmp");
113  }
114  /* set white to transparent on the happyface */
115  SDL_SetColorKey(bmp_surface, 1,
116  SDL_MapRGB(bmp_surface->format, 255, 255, 255));
117 
118  /* convert RGBA surface to texture */
119  texture = SDL_CreateTextureFromSurface(renderer, bmp_surface);
120  if (texture == 0) {
121  fatalError("could not create texture");
122  }
124 
125  /* free up allocated memory */
126  SDL_FreeSurface(bmp_surface);
127 }
#define SDL_LoadBMP(file)
Definition: SDL_surface.h:200
void fatalError(const char *string)
Definition: common.c:32
A collection of pixels used in software blitting.
Definition: SDL_surface.h:69
#define SDL_SetTextureBlendMode
GLenum GLenum GLuint texture
#define SDL_CreateTextureFromSurface
#define SDL_FreeSurface
#define SDL_SetColorKey
#define NULL
Definition: begin_code.h:164
SDL_PixelFormat * format
Definition: SDL_surface.h:72
#define SDL_MapRGB

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 130 of file happy.c.

References done, fatalError(), initializeHappyFaces(), initializeTexture(), NULL, render(), renderer, SDL_CreateRenderer, SDL_CreateWindow, SDL_Delay, SDL_DestroyTexture, SDL_GetWindowSize, SDL_Init, SDL_INIT_VIDEO, SDL_PollEvent, SDL_Quit, SDL_QUIT, SDL_RenderSetLogicalSize, SDL_WINDOW_ALLOW_HIGHDPI, SDL_WINDOW_FULLSCREEN, SDL_Event::type, and updateDeltaTime().

131 {
134  int done;
135  int width;
136  int height;
137 
138  /* initialize SDL */
139  if (SDL_Init(SDL_INIT_VIDEO) < 0) {
140  fatalError("Could not initialize SDL");
141  }
142 
143  /* The specified window size doesn't matter - except for its aspect ratio,
144  * which determines whether the window is in portrait or landscape on iOS
145  * (if SDL_WINDOW_RESIZABLE isn't specified). */
147 
148  renderer = SDL_CreateRenderer(window, -1, 0);
149 
150  SDL_GetWindowSize(window, &width, &height);
151  SDL_RenderSetLogicalSize(renderer, width, height);
152 
153  initializeTexture(renderer);
154  initializeHappyFaces(renderer);
155 
156 
157  /* main loop */
158  done = 0;
159  while (!done) {
161  double deltaTime = updateDeltaTime();
162 
163  while (SDL_PollEvent(&event)) {
164  if (event.type == SDL_QUIT) {
165  done = 1;
166  }
167  }
168 
169  render(renderer, deltaTime);
170  SDL_Delay(1);
171  }
172 
173  /* cleanup */
175  /* shutdown SDL */
176  SDL_Quit();
177 
178  return 0;
179 
180 }
#define SDL_PollEvent
double updateDeltaTime(void)
Definition: common.c:42
void fatalError(const char *string)
Definition: common.c:32
#define SDL_CreateWindow
GLint GLint GLsizei width
Definition: SDL_opengl.h:1572
#define SDL_GetWindowSize
GLenum GLenum GLuint texture
static SDL_Renderer * renderer
struct _cl_event * event
#define SDL_RenderSetLogicalSize
#define SDL_Quit
int done
Definition: checkkeys.c:28
void render(SDL_Renderer *renderer, double deltaTime)
Definition: happy.c:42
#define SDL_Delay
#define NULL
Definition: begin_code.h:164
GLint GLint GLsizei GLsizei height
Definition: SDL_opengl.h:1572
void initializeTexture(SDL_Renderer *renderer)
Definition: happy.c:106
EGLSurface EGLNativeWindowType * window
Definition: eglext.h:1025
#define SDL_DestroyTexture
The type used to identify a window.
Definition: SDL_sysvideo.h:73
#define SDL_Init
General event structure.
Definition: SDL_events.h:557
void initializeHappyFaces(SDL_Renderer *renderer)
Definition: happy.c:26
#define SDL_INIT_VIDEO
Definition: SDL.h:79
#define SDL_CreateRenderer
Uint32 type
Definition: SDL_events.h:559

◆ render()

void render ( SDL_Renderer renderer,
double  deltaTime 
)

Definition at line 42 of file happy.c.

References faces, SDL_Rect::h, HAPPY_FACE_SIZE, i, NUM_HAPPY_FACES, SDL_RenderClear, SDL_RenderCopy, SDL_RenderGetLogicalSize, SDL_RenderPresent, SDL_SetRenderDrawColor, SDL_Rect::w, SDL_Rect::x, and SDL_Rect::y.

Referenced by main().

43 {
44  int i;
45  SDL_Rect srcRect;
46  SDL_Rect dstRect;
47  int w;
48  int h;
49 
50  SDL_RenderGetLogicalSize(renderer, &w, &h);
51 
52  /* setup boundaries for happyface bouncing */
53  int maxx = w - HAPPY_FACE_SIZE;
54  int maxy = h - HAPPY_FACE_SIZE;
55  int minx = 0;
56  int miny = 0;
57 
58  /* setup rects for drawing */
59  srcRect.x = 0;
60  srcRect.y = 0;
61  srcRect.w = HAPPY_FACE_SIZE;
62  srcRect.h = HAPPY_FACE_SIZE;
63  dstRect.w = HAPPY_FACE_SIZE;
64  dstRect.h = HAPPY_FACE_SIZE;
65 
66  /* fill background in with black */
67  SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
68  SDL_RenderClear(renderer);
69 
70  /*
71  loop through all the happy faces:
72  - update position
73  - update velocity (if boundary is hit)
74  - draw
75  */
76  for (i = 0; i < NUM_HAPPY_FACES; i++) {
77  faces[i].x += faces[i].xvel * deltaTime;
78  faces[i].y += faces[i].yvel * deltaTime;
79  if (faces[i].x > maxx) {
80  faces[i].x = maxx;
81  faces[i].xvel = -faces[i].xvel;
82  } else if (faces[i].y > maxy) {
83  faces[i].y = maxy;
84  faces[i].yvel = -faces[i].yvel;
85  }
86  if (faces[i].x < minx) {
87  faces[i].x = minx;
88  faces[i].xvel = -faces[i].xvel;
89  } else if (faces[i].y < miny) {
90  faces[i].y = miny;
91  faces[i].yvel = -faces[i].yvel;
92  }
93  dstRect.x = faces[i].x;
94  dstRect.y = faces[i].y;
95  SDL_RenderCopy(renderer, texture, &srcRect, &dstRect);
96  }
97  /* update screen */
98  SDL_RenderPresent(renderer);
99 
100 }
#define HAPPY_FACE_SIZE
Definition: happy.c:11
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1574
GLfloat GLfloat GLfloat GLfloat h
#define SDL_RenderCopy
GLenum GLenum GLuint texture
GLubyte GLubyte GLubyte GLubyte w
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1574
int x
Definition: SDL_rect.h:66
int w
Definition: SDL_rect.h:67
static struct @62 faces[NUM_HAPPY_FACES]
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
#define NUM_HAPPY_FACES
Definition: happy.c:10
#define SDL_RenderClear
int h
Definition: SDL_rect.h:67
#define SDL_SetRenderDrawColor
#define SDL_RenderGetLogicalSize
int y
Definition: SDL_rect.h:66
A rectangle, with the origin at the upper left.
Definition: SDL_rect.h:64
#define SDL_RenderPresent

Variable Documentation

◆ faces

struct { ... } faces[NUM_HAPPY_FACES]

Referenced by initializeHappyFaces(), and render().

◆ texture

SDL_Texture* texture = 0
static

Definition at line 13 of file happy.c.

◆ x

float x

Definition at line 17 of file happy.c.

◆ xvel

float xvel

Definition at line 18 of file happy.c.

◆ y

float y

Definition at line 17 of file happy.c.

◆ yvel

float yvel

Definition at line 18 of file happy.c.