-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirstgrid.cpp
193 lines (156 loc) · 4.65 KB
/
firstgrid.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include "SDL/SDL.h"
#include <string>
//The attributes of the screen
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
// Grid attributes
const int GRID_SIZE = 20;
// For events
SDL_Event event;
//The surfaces that will be used
SDL_Surface *screen = NULL;
// Functions declared below
void draw_grid(SDL_Surface *);
void draw_square(SDL_Surface *, int, int);
int main( int argc, char* args[] )
{
//Initialize all SDL subsystems
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
{
return 1;
}
//Set up the screen
screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
//If there was an error in setting up the screen
if( screen == NULL )
{
return 1;
}
//Set the window caption
SDL_WM_SetCaption( "Grid", NULL );
// Draw the grid
draw_grid(screen);
// Draw the square
draw_square(screen, 0, 0);
//Update the screen
if( SDL_Flip( screen ) == -1 )
{
return 1;
}
bool quit = false;
while (quit == false) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
quit = true;
}
}
SDL_Delay(100);
}
//Quit SDL
SDL_Quit();
return 0;
}
/*
* Set the pixel at (x, y) to the given value
* NOTE: The surface must be locked before calling this!
* NOTE: This is from:
* file:///c:/SDL-1.2.13/docs/html/guidevideo.html#GUIDEVIDEOINTRO
*/
void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
{
int bpp = surface->format->BytesPerPixel;
/* Here p is the address to the pixel we want to set */
Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
switch(bpp) {
case 1:
*p = pixel;
break;
case 2:
*(Uint16 *)p = pixel;
break;
case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
p[0] = (pixel >> 16) & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = pixel & 0xff;
} else {
p[0] = pixel & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = (pixel >> 16) & 0xff;
}
break;
case 4:
*(Uint32 *)p = pixel;
break;
}
}
/*
* Draws a grid on the surface
* NOTE: Based on:
* file:///c:/SDL-1.2.13/docs/html/guidevideo.html#GUIDEVIDEOINTRO
*/
void draw_grid(SDL_Surface *surface) {
/* Code to set a yellow pixel at the center of the screen */
Uint32 yellow;
/* Map the color yellow to this display (R=0xff, G=0xFF, B=0x00)
* Note: If the display is palettized, you must set the palette first.
* NOTE: Softened the color.
*/
yellow = SDL_MapRGB(surface->format, 0xcc, 0xcc, 0x00);
/* Lock the screen for direct access to the pixels */
if ( SDL_MUSTLOCK(surface) ) {
if ( SDL_LockSurface(surface) < 0 ) {
fprintf(stderr, "Can't lock screen: %s\n", SDL_GetError());
return;
}
}
for (int y = 0; y < surface->h; y+=1) {
for (int x = 0; x < surface->w; x+= 1) {
if ((y+1)%GRID_SIZE==0) {
putpixel(surface, x, y, yellow);
}
if ((x+1)%GRID_SIZE==0) {
putpixel(surface, x, y, yellow);
}
}
}
if (SDL_MUSTLOCK(surface)) {
SDL_UnlockSurface(surface);
}
/* Update "everything" */
SDL_UpdateRect(surface, 0, 0, 0, 0);
return;
}
/*
* Draws a square on the surface
* NOTE: Based on:
* file:///c:/SDL-1.2.13/docs/html/guidevideo.html#GUIDEVIDEOINTRO
*/
void draw_square(SDL_Surface *surface, int start_x, int start_y) {
/* Code to set a yellow pixel at the center of the screen */
Uint32 blue;
/* Map the color yellow to this display (R=0xff, G=0xFF, B=0x00)
* Note: If the display is palettized, you must set the palette first.
*/
blue = SDL_MapRGB(surface->format, 0x00, 0x00, 0xff);
/* Lock the screen for direct access to the pixels */
if ( SDL_MUSTLOCK(surface) ) {
if ( SDL_LockSurface(surface) < 0 ) {
fprintf(stderr, "Can't lock screen: %s\n", SDL_GetError());
return;
}
}
// Draw square here
for (int y = 0; y < GRID_SIZE; y+=1) {
for (int x = 0; x < GRID_SIZE; x+= 1) {
putpixel(surface, x+start_x, y+start_y, blue);
}
}
if (SDL_MUSTLOCK(surface)) {
SDL_UnlockSurface(surface);
}
/* Update "everything" */
SDL_UpdateRect(surface, 0, 0, 0, 0);
return;
}