Skip to content

Commit

Permalink
Merge pull request #4 from McrRaspJam/surface
Browse files Browse the repository at this point in the history
Simplified code by making Canvas an SDL_Surface
  • Loading branch information
Jack Kelly authored Oct 19, 2018
2 parents 954cd03 + e24b91e commit d8c78fa
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 238 deletions.
10 changes: 5 additions & 5 deletions examples/4_splash.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

int main(int argc, char *argv[])
{
int width = 64;
int height = 64;
int width = 256;
int height = 256;

canvas_create(width, height);

Expand All @@ -25,9 +25,9 @@ int main(int argc, char *argv[])
for (y = 0; y < height; y++)
for (x = 0; x < width; x++)
{
Uint8 r = 255 - ((255 / height) * y);
Uint8 g = (255 / width) * x;
Uint8 b = (255 / height) * y;
Uint8 r = 255 - ((255.0 / height) * y);
Uint8 g = (255.0 / width) * x;
Uint8 b = (255.0 / height) * y;

draw_pixel(x, y, r, g, b);
}
Expand Down
8 changes: 4 additions & 4 deletions program.c → main.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

int main(int argc, char *argv[])
{
int width = 32;
int height = 32;
int width = 64;
int height = 64;

canvas_create(width, height);

Expand All @@ -35,8 +35,8 @@ int main(int argc, char *argv[])
for (y = 0; y < height; y++)
for (x = 0; x < width; x++)
{
Uint8 r = 255 - ((255 / height) * y);
Uint8 g = (255 / width) * x;
Uint8 r = 255 - ((255.0 / height) * y);
Uint8 g = (255.0 / width) * x;
Uint8 b = frame % 255;

draw_pixel(x, y, r, g, b);
Expand Down
92 changes: 29 additions & 63 deletions pixels/canvas.c
Original file line number Diff line number Diff line change
@@ -1,82 +1,48 @@
/* canvas.c -- The Canvas
*
* The Canvas is an SDL surface which is used as the "virtual image" for user
* progams. It is scaled up to fit the window.
*
* Draw functions create pixels on the Canvas, and Screen updates blit the
* surface to the window
*/

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "SDL.h"

#include "canvas.h"

Canvas canvas;

Canvas * canvas_get()
{
return &canvas;
}
SDL_Surface * canvas;


/* Return false if coordinate lies outside canvas */
bool coordinate_validate(Coordinate *coordinate)
/* Initialise and allocate the Canvas */
void canvas_create(int width, int height)
{
bool valid = true;

if (coordinate->x < 0)
valid = false;
else if (coordinate->x >= canvas.size_x)
valid = false;
else if (coordinate->y < 0)
valid = false;
else if (coordinate->y >= canvas.size_y)
valid = false;

return valid;
if (canvas != NULL)
canvas_cleanup();

canvas = SDL_CreateRGBSurface(0,
width,
height,
32,
0xff000000,
0x00ff0000,
0x0000ff00,
0x000000ff);
}


/* Clip coordinate to canvas size */
void coordinate_bound(Coordinate *coordinate)
/* Variable accessor */
SDL_Surface * canvas_get()
{
if (coordinate->x < 0)
coordinate->x = 0;
else if (coordinate->x >= canvas.size_x)
coordinate->x = canvas.size_x - 1;

if (coordinate->y < 0)
coordinate->y = 0;
else if (coordinate->y >= canvas.size_y)
coordinate->y = canvas.size_y - 1;
return canvas;
}


/* Return pointer to a canvas pixel */
struct canvas_pixel * canvas_getpixel(Coordinate coordinate)
/* Free the Canvas */
void canvas_cleanup()
{
coordinate_bound(&coordinate);
return canvas.pixels + ((coordinate.y * canvas.size_y) + coordinate.x);
}

/*
* Allocate the canvas array to a given pixel value
*/
void canvas_create(int width, int height)
{
if (canvas.allocated == true) {
free(canvas.pixels);
canvas.allocated = false;
}

struct canvas_pixel defaultpixel;
defaultpixel.r = 0x00;
defaultpixel.g = 0x00;
defaultpixel.b = 0x00;

canvas.size_x = width;
canvas.size_y = height;

canvas.pixels = (struct canvas_pixel *) malloc((width * height) * sizeof(struct canvas_pixel));
canvas.allocated = true;

int i;
int j;
for (j = 0; j < height; j++)
for (i = 0; i < width; i++)
*(canvas.pixels + ((j * width) + i)) = defaultpixel;
SDL_FreeSurface(canvas);
}
27 changes: 2 additions & 25 deletions pixels/canvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,8 @@

#define CANVAS_H_INCLUDED

struct canvas_struct {
int size_x;
int size_y;
struct canvas_pixel *pixels;
bool allocated;
};

struct canvas_coordinate {
int x;
int y;
};

struct canvas_pixel {
Uint8 r;
Uint8 g;
Uint8 b;
};

typedef struct canvas_struct Canvas;
typedef struct canvas_coordinate Coordinate;

Canvas * canvas_get();
bool coordinate_validate(Coordinate *coordinate);
void coordinate_bound(Coordinate *coordinate);
struct canvas_pixel * canvas_getpixel(Coordinate coordinate);
void canvas_create(int width, int height);
SDL_Surface * canvas_get();
void canvas_cleanup();

#endif
77 changes: 31 additions & 46 deletions pixels/draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,69 +6,54 @@

#include <stdbool.h>
#include "SDL.h"

#include "canvas.h"
#include "screen.h"


/* Draw a single pixel on the canvas */
void draw_pixel(int x, int y, Uint8 r, Uint8 g, Uint8 b)
{
struct canvas_coordinate coord;
coord.x = x;
coord.y = y;

if (coordinate_validate(&coord) == false)
return;

struct canvas_pixel *pixel = canvas_getpixel(coord);
SDL_Renderer * renderer = screen_getrenderer();

pixel->r = r;
pixel->g = g;
pixel->b = b;
SDL_SetRenderDrawColor(renderer, r, g, b, 0xFF);
SDL_RenderDrawPoint(renderer, x, y);
}


/* Draw a rectangle of pixels on the canvas */
void draw_rectangle(int x1, int y1, int x2, int y2, Uint8 r, Uint8 g, Uint8 b)
{
int x, y;
struct canvas_coordinate coord;
struct canvas_pixel * pixel;

for (y = y1; y < y2; y++)
for (x = x1; x < x2; x++) {
coord.x = x;
coord.y = y;

if (coordinate_validate(&coord) == false)
continue;

pixel = canvas_getpixel(coord);

pixel->r = r;
pixel->g = g;
pixel->b = b;
}
SDL_Rect rect;

if (x2 > x1) {
rect.x = x1;
rect.w = x2 - x1;
} else {
rect.x = x2;
rect.w = x1 - x2;
}

if (y2 > y1) {
rect.y = y1;
rect.h = y2 - y1;
} else {
rect.y = y2;
rect.h = y1 - y2;
}

SDL_Renderer * renderer = screen_getrenderer();

SDL_SetRenderDrawColor(renderer, r, g, b, 0xFF);
SDL_RenderDrawRect(renderer, &rect);
}


/* Draw over the whole canvas */
void clear(Uint8 r, Uint8 g, Uint8 b)
{
Canvas *canvas = canvas_get();

int x, y;
struct canvas_coordinate coord;
struct canvas_pixel * pixel;

for (y = 0; y < canvas->size_y; y++)
for (x = 0; x < canvas->size_x; x++) {
coord.x = x;
coord.y = y;

pixel = canvas_getpixel(coord);

pixel->r = r;
pixel->g = g;
pixel->b = b;
}
SDL_Renderer * renderer = screen_getrenderer();

SDL_SetRenderDrawColor(renderer, r, g, b, 0xFF);
SDL_RenderClear(renderer);
}
Loading

0 comments on commit d8c78fa

Please sign in to comment.