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
|
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <SDL/SDL_image.h>
#include <SDL/SDL.h>
#include <SDL/SDL_gfxPrimitives.h>
using namespace std;
Uint32 getpixel(SDL_Surface *surface, int x, int y);
Uint32 getcolor(SDL_Surface *surface, int x, int y);
int main(void) {
SDL_Surface *screen;
SDL_Event event;
Uint32 farbe = 0xff0000ff;
int x, y;
bool quit = false;
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
cout << "Konnte SDL nicht initialisieren: %s\n" << SDL_GetError();
return EXIT_FAILURE;
}
atexit(SDL_Quit);
screen = SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE);
if (screen == NULL) {
cout << "Videomodus konnte nicht eingerichet werden: " << SDL_GetError()
<< endl;
return EXIT_FAILURE;
}
x = (screen->w / 2);
y = (screen->h / 2);
boxRGBA(screen, 0, 0, 800, 600, 255, 255, 255, 255);
SDL_WM_SetCaption("SDL_MapRGBA Test", 0);
filledCircleColor(screen, x, y, 250, farbe);
SDL_UpdateRect(screen, 0, 0, 0, 0);
cout << "Referenzfarbe: " << hex << farbe << endl;
cout << "Ausgelesene Farbe: " << hex << getcolor(screen, x, y) << endl;
while (!quit) {
if (SDL_WaitEvent(&event) && !quit) {
switch (event.type) {
case SDL_KEYDOWN:
switch (event.key.keysym.sym) {
case SDLK_ESCAPE:
quit = 1;
break;
default:
break;
}
break;
}
}
}
return EXIT_SUCCESS;
}
Uint32 getcolor(SDL_Surface *surface, int x, int y) {
Uint8 r, g, b, a;
SDL_LockSurface(surface);
SDL_PixelFormat *format = surface->format;
Uint32 p = getpixel(surface, x, y);
SDL_GetRGBA(p, format, &r, &g, &b, &a);
SDL_UnlockSurface(surface);
cout << endl;
cout << "Ausgelesene einzelne 8-Bit Kanäle:" << endl;
cout << "r: " << dec << (int) r << " g: " << (int) g << " b: " << (int) b
<< " alpha: " << (int) a << endl;
cout << endl;
Uint32 result = SDL_MapRGBA(surface->format, r, g, b, a);
return result;
}
Uint32 getpixel(SDL_Surface *surface, int x, int y) {
int bpp = surface->format->BytesPerPixel;
/* Here p is the address to the pixel we want to retrieve */
Uint8 *p = (Uint8 *) surface->pixels + y * surface->pitch + x * bpp;
switch (bpp) {
case 1:
return *p;
break;
case 2:
return *(Uint16 *) p;
break;
case 3:
if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
return p[0] << 16 | p[1] << 8 | p[2];
else
return p[0] | p[1] << 8 | p[2] << 16;
break;
case 4:
return *(Uint32 *) p;
break;
default:
return 0; /* shouldn't happen, but avoids warnings */
}
}
|