This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
testcursor.c
260 lines (229 loc) · 6.51 KB
1
2
3
4
5
6
7
8
9
10
11
/*
Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely.
*/
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h>
#include <stdlib.h>
#include "SDL.h"
/* This is an example 16x16 cursor
top left : black
top right : inverted color or black
bottom left: white
bottom right: transparent
(swap left and right for different endianness)
*/
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
Uint16 cursor_data[16] = {
0xffff,
0xffff,
0xffff,
0xffff,
0xffff,
0xffff,
0xffff,
0xffff,
0x0000,
0x0000,
0x0000,
0x0000,
0x0000,
0x0000,
0x0000,
0x0000
45
46
};
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
Uint16 cursor_mask[16] = {
0xff00,
0xff00,
0xff00,
0xff00,
0xff00,
0xff00,
0xff00,
0xff00,
0xff00,
0xff00,
0xff00,
0xff00,
0xff00,
0xff00,
0xff00,
0xff00
67
68
};
69
70
/* another test cursor: smaller than 16x16, and with an odd height */
71
72
73
74
Uint8 small_cursor_data[11] =
{ 0x00, 0x18, 0x08, 0x38, 0x44, 0x54, 0x44, 0x38, 0x20, 0x20, 0x00 };
Uint8 small_cursor_mask[11] =
{ 0x3C, 0x3C, 0x3C, 0x7C, 0xC6, 0xC6, 0xC6, 0x7C, 0x78, 0x70, 0x70 };
75
76
77
/* XPM */
static const char *arrow[] = {
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
113
114
115
116
117
/* width height num_colors chars_per_pixel */
" 32 32 3 1",
/* colors */
"X c #000000",
". c #ffffff",
" c None",
/* pixels */
"X ",
"XX ",
"X.X ",
"X..X ",
"X...X ",
"X....X ",
"X.....X ",
"X......X ",
"X.......X ",
"X........X ",
"X.....XXXXX ",
"X..X..X ",
"X.X X..X ",
"XX X..X ",
"X X..X ",
" X..X ",
" X..X ",
" X..X ",
" XX ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
"0,0"
118
119
};
120
121
static SDL_Cursor *
create_arrow_cursor()
122
{
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
int i, row, col;
Uint8 data[4 * 32];
Uint8 mask[4 * 32];
int hot_x, hot_y;
i = -1;
for (row = 0; row < 32; ++row) {
for (col = 0; col < 32; ++col) {
if (col % 8) {
data[i] <<= 1;
mask[i] <<= 1;
} else {
++i;
data[i] = mask[i] = 0;
}
switch (arrow[4 + row][col]) {
case 'X':
data[i] |= 0x01;
mask[i] |= 0x01;
break;
case '.':
mask[i] |= 0x01;
break;
case ' ':
break;
}
}
150
}
151
SDL_sscanf(arrow[4 + row], "%d,%d", &hot_x, &hot_y);
152
return SDL_CreateCursor(data, mask, 32, 32, hot_x, hot_y);
153
154
}
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
SDL_Surface *
LoadSprite(char *file)
{
SDL_Surface *sprite;
/* Load the sprite image */
sprite = SDL_LoadBMP(file);
if (sprite == NULL) {
fprintf(stderr, "Couldn't load %s: %s", file, SDL_GetError());
return NULL;
}
/* Set transparent pixel as the pixel at (0,0) */
if (sprite->format->palette) {
SDL_SetColorKey(sprite, (SDL_SRCCOLORKEY | SDL_RLEACCEL),
*(Uint8 *) sprite->pixels);
}
/* We're ready to roll. :) */
return sprite;
}
176
177
178
int
main(int argc, char *argv[])
179
{
180
181
SDL_Surface *screen;
SDL_bool quit = SDL_FALSE, first_time = SDL_TRUE;
182
SDL_Cursor *cursor[5];
183
184
185
186
187
188
189
190
int current;
/* Load the SDL library */
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
return (1);
}
191
screen = SDL_SetVideoMode(640, 480, 8, SDL_ANYFORMAT);
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
if (screen == NULL) {
fprintf(stderr, "Couldn't initialize video mode: %s\n",
SDL_GetError());
return (1);
}
SDL_FillRect(screen, NULL, 0x664422);
cursor[0] = SDL_CreateCursor((Uint8 *) cursor_data, (Uint8 *) cursor_mask,
16, 16, 8, 8);
if (cursor[0] == NULL) {
fprintf(stderr, "Couldn't initialize test cursor: %s\n",
SDL_GetError());
SDL_Quit();
return (1);
}
cursor[1] = create_arrow_cursor();
if (cursor[1] == NULL) {
fprintf(stderr, "Couldn't initialize arrow cursor: %s\n",
SDL_GetError());
SDL_FreeCursor(cursor[0]);
SDL_Quit();
return (1);
}
cursor[2] = SDL_CreateCursor(small_cursor_data, small_cursor_mask,
8, 11, 3, 5);
if (cursor[2] == NULL) {
fprintf(stderr, "Couldn't initialize test cursor: %s\n",
SDL_GetError());
SDL_Quit();
return (1);
}
224
225
cursor[3] = SDL_CreateColorCursor(LoadSprite("icon.bmp"), 0, 0);
cursor[4] = SDL_GetCursor();
226
227
current = SDL_arraysize(cursor)-1;
228
229
230
231
232
233
234
SDL_SetCursor(cursor[current]);
while (!quit) {
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_MOUSEBUTTONDOWN:
235
current = (current + 1) % SDL_arraysize(cursor);
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
SDL_SetCursor(cursor[current]);
break;
case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_ESCAPE) {
quit = SDL_TRUE;
}
break;
case SDL_QUIT:
quit = SDL_TRUE;
break;
}
}
SDL_Flip(screen);
SDL_Delay(1);
}
252
253
254
for (current = 0; current < SDL_arraysize(cursor); ++current) {
SDL_FreeCursor(cursor[current]);
}
255
256
257
SDL_Quit();
return (0);
258
}
259
260
/* vi: set ts=4 sw=4 expandtab: */