-
Notifications
You must be signed in to change notification settings - Fork 0
/
SDLSample.cpp
65 lines (59 loc) · 1.83 KB
/
SDLSample.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "SDLSample.h"
// draw a text txt on surface screen, starting from the point (x, y)
// charset is a 128x128 bitmap containing character images
void DrawString(SDL_Surface *screen, int x, int y, const char *text,
SDL_Surface *charset) {
int px, py, c;
SDL_Rect s, d;
s.w = 8;
s.h = 8;
d.w = 8;
d.h = 8;
while (*text) {
c = *text & 255;
px = (c % 16) * 8;
py = (c / 16) * 8;
s.x = px;
s.y = py;
d.x = x;
d.y = y;
SDL_BlitSurface(charset, &s, screen, &d);
x += 8;
text++;
};
};
// draw a surface sprite on a surface screen in point (x, y)
// (x, y) is the center of sprite on screen
void DrawSurface(SDL_Surface *screen, SDL_Surface *sprite, int x, int y) {
SDL_Rect dest;
dest.x = x - sprite->w / 2;
dest.y = y - sprite->h / 2;
dest.w = sprite->w;
dest.h = sprite->h;
SDL_BlitSurface(sprite, NULL, screen, &dest);
};
// draw a single pixel
void DrawPixel(SDL_Surface *surface, int x, int y, Uint32 color) {
int bpp = surface->format->BytesPerPixel;
Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
*(Uint32 *)p = color;
};
// draw a vertical (when dx = 0, dy = 1) or horizontal (when dx = 1, dy = 0) line
void DrawLine(SDL_Surface *screen, int x, int y, int l, int dx, int dy, Uint32 color) {
for (int i = 0; i < l; i++) {
DrawPixel(screen, x, y, color);
x += dx;
y += dy;
};
};
// draw a rectangle of size l by k
void DrawRectangle(SDL_Surface *screen, int x, int y, int l, int k,
Uint32 outlineColor, Uint32 fillColor) {
int i;
DrawLine(screen, x, y, k, 0, 1, outlineColor);
DrawLine(screen, x + l - 1, y, k, 0, 1, outlineColor);
DrawLine(screen, x, y, l, 1, 0, outlineColor);
DrawLine(screen, x, y + k - 1, l, 1, 0, outlineColor);
for (i = y + 1; i < y + k - 1; i++)
DrawLine(screen, x + 1, i, l - 2, 1, 0, fillColor);
};