Skip to content

Commit d8c78fa

Browse files
author
Jack Kelly
authored
Merge pull request #4 from McrRaspJam/surface
Simplified code by making Canvas an SDL_Surface
2 parents 954cd03 + e24b91e commit d8c78fa

File tree

8 files changed

+123
-238
lines changed

8 files changed

+123
-238
lines changed

examples/4_splash.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
int main(int argc, char *argv[])
1313
{
14-
int width = 64;
15-
int height = 64;
14+
int width = 256;
15+
int height = 256;
1616

1717
canvas_create(width, height);
1818

@@ -25,9 +25,9 @@ int main(int argc, char *argv[])
2525
for (y = 0; y < height; y++)
2626
for (x = 0; x < width; x++)
2727
{
28-
Uint8 r = 255 - ((255 / height) * y);
29-
Uint8 g = (255 / width) * x;
30-
Uint8 b = (255 / height) * y;
28+
Uint8 r = 255 - ((255.0 / height) * y);
29+
Uint8 g = (255.0 / width) * x;
30+
Uint8 b = (255.0 / height) * y;
3131

3232
draw_pixel(x, y, r, g, b);
3333
}

program.c renamed to main.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
int main(int argc, char *argv[])
1313
{
14-
int width = 32;
15-
int height = 32;
14+
int width = 64;
15+
int height = 64;
1616

1717
canvas_create(width, height);
1818

@@ -35,8 +35,8 @@ int main(int argc, char *argv[])
3535
for (y = 0; y < height; y++)
3636
for (x = 0; x < width; x++)
3737
{
38-
Uint8 r = 255 - ((255 / height) * y);
39-
Uint8 g = (255 / width) * x;
38+
Uint8 r = 255 - ((255.0 / height) * y);
39+
Uint8 g = (255.0 / width) * x;
4040
Uint8 b = frame % 255;
4141

4242
draw_pixel(x, y, r, g, b);

pixels/canvas.c

Lines changed: 29 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,48 @@
1+
/* canvas.c -- The Canvas
2+
*
3+
* The Canvas is an SDL surface which is used as the "virtual image" for user
4+
* progams. It is scaled up to fit the window.
5+
*
6+
* Draw functions create pixels on the Canvas, and Screen updates blit the
7+
* surface to the window
8+
*/
9+
110
#include <stdio.h>
211
#include <stdlib.h>
312
#include <stdbool.h>
413
#include "SDL.h"
514

615
#include "canvas.h"
716

8-
Canvas canvas;
9-
10-
Canvas * canvas_get()
11-
{
12-
return &canvas;
13-
}
17+
SDL_Surface * canvas;
1418

1519

16-
/* Return false if coordinate lies outside canvas */
17-
bool coordinate_validate(Coordinate *coordinate)
20+
/* Initialise and allocate the Canvas */
21+
void canvas_create(int width, int height)
1822
{
19-
bool valid = true;
20-
21-
if (coordinate->x < 0)
22-
valid = false;
23-
else if (coordinate->x >= canvas.size_x)
24-
valid = false;
25-
else if (coordinate->y < 0)
26-
valid = false;
27-
else if (coordinate->y >= canvas.size_y)
28-
valid = false;
29-
30-
return valid;
23+
if (canvas != NULL)
24+
canvas_cleanup();
25+
26+
canvas = SDL_CreateRGBSurface(0,
27+
width,
28+
height,
29+
32,
30+
0xff000000,
31+
0x00ff0000,
32+
0x0000ff00,
33+
0x000000ff);
3134
}
3235

3336

34-
/* Clip coordinate to canvas size */
35-
void coordinate_bound(Coordinate *coordinate)
37+
/* Variable accessor */
38+
SDL_Surface * canvas_get()
3639
{
37-
if (coordinate->x < 0)
38-
coordinate->x = 0;
39-
else if (coordinate->x >= canvas.size_x)
40-
coordinate->x = canvas.size_x - 1;
41-
42-
if (coordinate->y < 0)
43-
coordinate->y = 0;
44-
else if (coordinate->y >= canvas.size_y)
45-
coordinate->y = canvas.size_y - 1;
40+
return canvas;
4641
}
4742

4843

49-
/* Return pointer to a canvas pixel */
50-
struct canvas_pixel * canvas_getpixel(Coordinate coordinate)
44+
/* Free the Canvas */
45+
void canvas_cleanup()
5146
{
52-
coordinate_bound(&coordinate);
53-
return canvas.pixels + ((coordinate.y * canvas.size_y) + coordinate.x);
54-
}
55-
56-
/*
57-
* Allocate the canvas array to a given pixel value
58-
*/
59-
void canvas_create(int width, int height)
60-
{
61-
if (canvas.allocated == true) {
62-
free(canvas.pixels);
63-
canvas.allocated = false;
64-
}
65-
66-
struct canvas_pixel defaultpixel;
67-
defaultpixel.r = 0x00;
68-
defaultpixel.g = 0x00;
69-
defaultpixel.b = 0x00;
70-
71-
canvas.size_x = width;
72-
canvas.size_y = height;
73-
74-
canvas.pixels = (struct canvas_pixel *) malloc((width * height) * sizeof(struct canvas_pixel));
75-
canvas.allocated = true;
76-
77-
int i;
78-
int j;
79-
for (j = 0; j < height; j++)
80-
for (i = 0; i < width; i++)
81-
*(canvas.pixels + ((j * width) + i)) = defaultpixel;
47+
SDL_FreeSurface(canvas);
8248
}

pixels/canvas.h

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,8 @@
22

33
#define CANVAS_H_INCLUDED
44

5-
struct canvas_struct {
6-
int size_x;
7-
int size_y;
8-
struct canvas_pixel *pixels;
9-
bool allocated;
10-
};
11-
12-
struct canvas_coordinate {
13-
int x;
14-
int y;
15-
};
16-
17-
struct canvas_pixel {
18-
Uint8 r;
19-
Uint8 g;
20-
Uint8 b;
21-
};
22-
23-
typedef struct canvas_struct Canvas;
24-
typedef struct canvas_coordinate Coordinate;
25-
26-
Canvas * canvas_get();
27-
bool coordinate_validate(Coordinate *coordinate);
28-
void coordinate_bound(Coordinate *coordinate);
29-
struct canvas_pixel * canvas_getpixel(Coordinate coordinate);
305
void canvas_create(int width, int height);
6+
SDL_Surface * canvas_get();
7+
void canvas_cleanup();
318

329
#endif

pixels/draw.c

Lines changed: 31 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,69 +6,54 @@
66

77
#include <stdbool.h>
88
#include "SDL.h"
9+
910
#include "canvas.h"
11+
#include "screen.h"
1012

1113

1214
/* Draw a single pixel on the canvas */
1315
void draw_pixel(int x, int y, Uint8 r, Uint8 g, Uint8 b)
1416
{
15-
struct canvas_coordinate coord;
16-
coord.x = x;
17-
coord.y = y;
18-
19-
if (coordinate_validate(&coord) == false)
20-
return;
21-
22-
struct canvas_pixel *pixel = canvas_getpixel(coord);
17+
SDL_Renderer * renderer = screen_getrenderer();
2318

24-
pixel->r = r;
25-
pixel->g = g;
26-
pixel->b = b;
19+
SDL_SetRenderDrawColor(renderer, r, g, b, 0xFF);
20+
SDL_RenderDrawPoint(renderer, x, y);
2721
}
2822

2923

3024
/* Draw a rectangle of pixels on the canvas */
3125
void draw_rectangle(int x1, int y1, int x2, int y2, Uint8 r, Uint8 g, Uint8 b)
3226
{
33-
int x, y;
34-
struct canvas_coordinate coord;
35-
struct canvas_pixel * pixel;
36-
37-
for (y = y1; y < y2; y++)
38-
for (x = x1; x < x2; x++) {
39-
coord.x = x;
40-
coord.y = y;
41-
42-
if (coordinate_validate(&coord) == false)
43-
continue;
44-
45-
pixel = canvas_getpixel(coord);
46-
47-
pixel->r = r;
48-
pixel->g = g;
49-
pixel->b = b;
50-
}
27+
SDL_Rect rect;
28+
29+
if (x2 > x1) {
30+
rect.x = x1;
31+
rect.w = x2 - x1;
32+
} else {
33+
rect.x = x2;
34+
rect.w = x1 - x2;
35+
}
36+
37+
if (y2 > y1) {
38+
rect.y = y1;
39+
rect.h = y2 - y1;
40+
} else {
41+
rect.y = y2;
42+
rect.h = y1 - y2;
43+
}
44+
45+
SDL_Renderer * renderer = screen_getrenderer();
46+
47+
SDL_SetRenderDrawColor(renderer, r, g, b, 0xFF);
48+
SDL_RenderDrawRect(renderer, &rect);
5149
}
5250

5351

5452
/* Draw over the whole canvas */
5553
void clear(Uint8 r, Uint8 g, Uint8 b)
5654
{
57-
Canvas *canvas = canvas_get();
58-
59-
int x, y;
60-
struct canvas_coordinate coord;
61-
struct canvas_pixel * pixel;
62-
63-
for (y = 0; y < canvas->size_y; y++)
64-
for (x = 0; x < canvas->size_x; x++) {
65-
coord.x = x;
66-
coord.y = y;
67-
68-
pixel = canvas_getpixel(coord);
69-
70-
pixel->r = r;
71-
pixel->g = g;
72-
pixel->b = b;
73-
}
55+
SDL_Renderer * renderer = screen_getrenderer();
56+
57+
SDL_SetRenderDrawColor(renderer, r, g, b, 0xFF);
58+
SDL_RenderClear(renderer);
7459
}

0 commit comments

Comments
 (0)