summaryrefslogtreecommitdiff
path: root/src/pixel.c
blob: abe756e9910c793b0580d4ed543372f739a18a42 (plain)
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
/*
** pixel.c for  in /home/gayot_o/prog/lib/sdl-digit
** 
** Made by olivier gayot
** Login   <gayot_o@epitech.net>
** 
** Started on  Mon Apr 23 08:13:46 2012 olivier gayot
** Last update Mon Apr 23 08:13:46 2012 olivier gayot
*/

#include	"sdl_digit.h"

void		set_pixel_color(SDL_Surface *surf,
    int x, int y, Uint32 color) {
  char		*address = surf->pixels;
  union_	cast_;

  if (x >= surf->w || y >= surf->h || x < 0 || y < 0)
    return;
  address += surf->pitch * y;
  address += x * surf->format->BytesPerPixel;
  switch (surf->format->BitsPerPixel) {
    case 32:
      cast_.int_ = (Uint32 *)address;
      *cast_.int_ = color;
      break;
    case 24:
      cast_.int_ = (Uint32 *)address;
      *cast_.int_ = color;
      break;
    case 16:
      cast_.short_ = (Uint16 *)address;
      *cast_.short_ = color;
      break;
    case 8:
      cast_.char_ = (Uint8 *)address;
      *cast_.char_ = color;
      break;
    default:
      return;
      break;
  }
}

Uint32		get_pixel_color(SDL_Surface *surf, int x, int y) {
  char		*address = surf->pixels;

  if (x >= surf->w || y >= surf->h || x < 0 || y < 0)
    return 0;
  address += surf->pitch * y;
  address += x * surf->format->BytesPerPixel;
  switch (surf->format->BitsPerPixel) {
    case 32:
      return ((Uint32)*((Uint32 *)address));
      break;
    case 24:
      return ((Uint32)*((Uint32 *)address));
      break;
    case 16:
      return ((Uint32)*((Uint16 *)address));
      break;
    case 8:
      return ((Uint32)*((Uint8 *)address));
      break;
    default:
      return ((Uint32)0);
      break;
  }
}