SDL  2.0
happy.c
Go to the documentation of this file.
1 /*
2  * happy.c
3  * written by Holmes Futrell
4  * use however you want
5  */
6 
7 #include "SDL.h"
8 #include "common.h"
9 
10 #define NUM_HAPPY_FACES 100 /* number of faces to draw */
11 #define HAPPY_FACE_SIZE 32 /* width and height of happyface */
12 
13 static SDL_Texture *texture = 0; /* reference to texture holding happyface */
14 
15 static struct
16 {
17  float x, y; /* position of happyface */
18  float xvel, yvel; /* velocity of happyface */
20 
21 /*
22  Sets initial positions and velocities of happyfaces
23  units of velocity are pixels per millesecond
24 */
25 void
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 }
40 
41 void
42 render(SDL_Renderer *renderer, double deltaTime)
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 }
101 
102 /*
103  loads the happyface graphic into a texture
104 */
105 void
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 }
128 
129 int
130 main(int argc, char *argv[])
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 */
174  SDL_DestroyTexture(texture);
175  /* shutdown SDL */
176  SDL_Quit();
177 
178  return 0;
179 
180 }
#define HAPPY_FACE_SIZE
Definition: happy.c:11
#define SDL_PollEvent
double updateDeltaTime(void)
Definition: common.c:42
float y
Definition: happy.c:17
#define SDL_LoadBMP(file)
Definition: SDL_surface.h:200
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1574
void fatalError(const char *string)
Definition: common.c:32
GLfloat GLfloat GLfloat GLfloat h
A collection of pixels used in software blitting.
Definition: SDL_surface.h:69
#define SDL_SetTextureBlendMode
GLfloat f
#define SDL_CreateWindow
float xvel
Definition: happy.c:18
GLint GLint GLsizei width
Definition: SDL_opengl.h:1572
#define SDL_RenderCopy
float randomFloat(float min, float max)
Definition: common.c:26
#define SDL_GetWindowSize
GLenum GLenum GLuint texture
#define SDL_CreateTextureFromSurface
int main(int argc, char *argv[])
Definition: happy.c:130
#define SDL_FreeSurface
static SDL_Renderer * renderer
struct _cl_event * event
#define SDL_RenderSetLogicalSize
#define SDL_Quit
int done
Definition: checkkeys.c:28
GLubyte GLubyte GLubyte GLubyte w
#define SDL_SetColorKey
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1574
int x
Definition: SDL_rect.h:66
void render(SDL_Renderer *renderer, double deltaTime)
Definition: happy.c:42
int w
Definition: SDL_rect.h:67
static struct @62 faces[NUM_HAPPY_FACES]
#define SDL_Delay
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 NULL
Definition: begin_code.h:164
#define NUM_HAPPY_FACES
Definition: happy.c:10
SDL_PixelFormat * format
Definition: SDL_surface.h:72
GLint GLint GLsizei GLsizei height
Definition: SDL_opengl.h:1572
#define SDL_RenderClear
void initializeTexture(SDL_Renderer *renderer)
Definition: happy.c:106
EGLSurface EGLNativeWindowType * window
Definition: eglext.h:1025
#define SDL_DestroyTexture
int h
Definition: SDL_rect.h:67
The type used to identify a window.
Definition: SDL_sysvideo.h:73
#define SDL_MapRGB
float x
Definition: happy.c:17
#define SDL_Init
General event structure.
Definition: SDL_events.h:557
float yvel
Definition: happy.c:18
#define SDL_SetRenderDrawColor
#define SDL_RenderGetLogicalSize
int y
Definition: SDL_rect.h:66
void initializeHappyFaces(SDL_Renderer *renderer)
Definition: happy.c:26
#define SDL_INIT_VIDEO
Definition: SDL.h:79
#define SDL_CreateRenderer
A rectangle, with the origin at the upper left.
Definition: SDL_rect.h:64
#define SDL_RenderPresent
Uint32 type
Definition: SDL_events.h:559