-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsurface.c
49 lines (39 loc) · 1.17 KB
/
surface.c
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
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include "surface.h"
#include "point.h"
SDL_Surface* load_surface(SDL_Surface* screen_surface, const char* path)
{
//The final optimized image
SDL_Surface* optimizedSurface = NULL;
//Load image at specified path
SDL_Surface* loadedSurface = IMG_Load(path);
if (loadedSurface == NULL) {
printf("Unable to load image %s: %s\n", path, IMG_GetError());
goto out;
}
//Convert surface to screen format
optimizedSurface = SDL_ConvertSurface(loadedSurface, screen_surface->format, 0);
if (optimizedSurface == NULL) {
printf("Unable to optimize image %s: %s\n", path, SDL_GetError());
}
out:
return optimizedSurface;
}
void put_red_pixel(SDL_Surface *surface, const struct point *pt)
{
Uint32* pixels = (Uint32*)surface->pixels;
pixels += pt->y * surface->w + pt->x;
*pixels = SDL_MapRGB(surface->format, 255, 0, 0);
}
struct pixel get_pixel(SDL_Surface *surface, const struct point *pt)
{
struct pixel pix;
Uint32* pixels = (Uint32*)surface->pixels;
pixels += pt->y * surface->w + pt->x;
Uint8* pixel_bytes = (Uint8*) pixels;
pix.r = pixel_bytes[0];
pix.g = pixel_bytes[1];
pix.b = pixel_bytes[2];
return pix;
}