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 113 114 115 116 117 118 119 120 121 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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
|
/*
* Bit depth independent backend using allgro (mainly for BeOS)
*
* This file is part of the Simutrans project under the artistic license.
*/
#include <stdlib.h>
#include <stdio.h>
#include "macros.h"
#include "simsys.h"
#include "simevent.h"
#include "simgraph.h"
#include "simsys_w32_png.h"
#include "simversion.h"
#include <allegro.h>
static void simtimer_init(void);
static int width = 800;
static int height = 600;
/* event-handling */
/* queue Eintraege sind wie folgt aufgebaut
* 4 integer werte
* class
* code
* mx
* my
*/
#define queue_length 4096
#define queue_items 6
static volatile unsigned int event_top_mark = 0;
static volatile unsigned int event_bot_mark = 0;
static volatile int event_queue[queue_length * queue_items + 8];
#define INSERT_EVENT(a, b) \
event_queue[event_top_mark++] = a; \
event_queue[event_top_mark++] = b; \
event_queue[event_top_mark++] = mouse_x; \
event_queue[event_top_mark++] = mouse_y; \
event_queue[event_top_mark++] = mouse_b; \
event_queue[event_top_mark++] = mouse_z; \
if (event_top_mark >= queue_length * queue_items) event_top_mark = 0;
void my_mouse_callback(int flags)
{
if (flags & MOUSE_FLAG_MOVE) {
INSERT_EVENT(SIM_MOUSE_MOVE, SIM_MOUSE_MOVED)
}
if (flags & MOUSE_FLAG_LEFT_DOWN) {
INSERT_EVENT(SIM_MOUSE_BUTTONS, SIM_MOUSE_LEFTBUTTON)
}
if (flags & MOUSE_FLAG_MIDDLE_DOWN) {
INSERT_EVENT(SIM_MOUSE_BUTTONS, SIM_MOUSE_MIDBUTTON)
}
if (flags & MOUSE_FLAG_RIGHT_DOWN) {
INSERT_EVENT(SIM_MOUSE_BUTTONS, SIM_MOUSE_RIGHTBUTTON)
}
if (flags & MOUSE_FLAG_LEFT_UP) {
INSERT_EVENT(SIM_MOUSE_BUTTONS, SIM_MOUSE_LEFTUP)
}
if (flags & MOUSE_FLAG_MIDDLE_UP) {
INSERT_EVENT(SIM_MOUSE_BUTTONS, SIM_MOUSE_MIDUP)
}
if (flags & MOUSE_FLAG_RIGHT_UP) {
INSERT_EVENT(SIM_MOUSE_BUTTONS, SIM_MOUSE_RIGHTUP)
}
if (flags & MOUSE_FLAG_MOVE_Z) {
if(event_top_mark > 0) {
if (event_queue[event_top_mark-1] < mouse_z) {
INSERT_EVENT(SIM_MOUSE_BUTTONS, SIM_MOUSE_WHEELUP)
}
else if (event_queue[event_top_mark-1] > mouse_z) {
INSERT_EVENT(SIM_MOUSE_BUTTONS, SIM_MOUSE_WHEELDOWN)
}
}
}
}
END_OF_FUNCTION(my_mouse_callback)
static int my_keyboard_callback(int this_key, int* scancode)
{
if (this_key > 0) {
// seems to be ASCII
INSERT_EVENT(SIM_KEYBOARD, this_key);
*scancode = 0;
return 0;
}
int numlock = (key_shifts & KB_NUMLOCK_FLAG) != 0;
switch (*scancode) {
case KEY_PGUP: this_key = '>'; break;
case KEY_PGDN: this_key = '<'; break;
case KEY_HOME: this_key = SIM_KEY_HOME; break;
case KEY_END: this_key = SIM_KEY_END; break;
case KEY_DOWN: this_key = SIM_KEY_DOWN; break;
case KEY_LEFT: this_key = SIM_KEY_LEFT; break;
case KEY_RIGHT: this_key = SIM_KEY_RIGHT; break;
case KEY_UP: this_key = SIM_KEY_UP; break;
case KEY_0_PAD: this_key = '0'; break;
case KEY_1_PAD: this_key = '1'; break;
case KEY_2_PAD: this_key = numlock ? '2' : SIM_KEY_DOWN; break;
case KEY_3_PAD: this_key = '3'; break;
case KEY_4_PAD: this_key = numlock ? '4' : SIM_KEY_LEFT; break;
case KEY_5_PAD: this_key = '5'; break;
case KEY_6_PAD: this_key = numlock ? '6' : SIM_KEY_RIGHT; break;
case KEY_7_PAD: this_key = '7'; break;
case KEY_8_PAD: this_key = numlock ? '8' : SIM_KEY_UP; break;
case KEY_9_PAD: this_key = '9'; break;
case KEY_F1: this_key = SIM_KEY_F1; break;
case KEY_F2: this_key = SIM_KEY_F2; break;
case KEY_F3: this_key = SIM_KEY_F3; break;
case KEY_F4: this_key = SIM_KEY_F4; break;
case KEY_F5: this_key = SIM_KEY_F5; break;
case KEY_F6: this_key = SIM_KEY_F6; break;
case KEY_F7: this_key = SIM_KEY_F7; break;
case KEY_F8: this_key = SIM_KEY_F8; break;
case KEY_F9: this_key = SIM_KEY_F9; break;
case KEY_F10: this_key = SIM_KEY_F10; break;
case KEY_F11: this_key = SIM_KEY_F11; break;
case KEY_F12: this_key = SIM_KEY_F12; break;
case KEY_ENTER_PAD: this_key = 13; break;
case KEY_DEL: this_key = 127; break;
case KEY_PAUSE: this_key = 16; break;
case KEY_LSHIFT:
case KEY_RSHIFT:
case KEY_LCONTROL:
case KEY_RCONTROL:
case KEY_NUMLOCK:
return this_key;
default:
*scancode = 0;
return 0;
}
INSERT_EVENT(SIM_KEYBOARD, this_key);
*scancode = 0;
return 0;
}
END_OF_FUNCTION(my_keyboard_callback)
void my_close_button_callback(void)
{
INSERT_EVENT(SIM_SYSTEM, SIM_SYSTEM_QUIT)
}
END_OF_FUNCTION(my_close_button_callback)
/*
* Hier sind die Basisfunktionen zur Initialisierung der Schnittstelle untergebracht
* -> init,open,close
*/
bool dr_os_init(int const* parameter)
{
if (allegro_init() != 0) {
dr_fatal_notify("Could not init Allegro.\n");
return false;
}
// prepare for next event
sys_event.type = SIM_NOEVENT;
sys_event.code = 0;
LOCK_VARIABLE(event_top_mark);
LOCK_VARIABLE(event_bot_mark);
LOCK_VARIABLE(event_queue);
LOCK_FUNCTION(my_mouse_callback);
LOCK_FUNCTION(my_keyboard_callback);
LOCK_FUNCTION(my_close_button_callback);
set_close_button_callback(my_close_button_callback);
simtimer_init();
return true;
}
resolution dr_query_screen_resolution()
{
resolution res;
if (get_desktop_resolution(&res.w, &res.h) != 0) {
res.w = width;
res.h = height;
}
return res;
}
int dr_os_open(int const w, int const h, int const fullscreen)
{
width = w;
height = h;
install_keyboard();
set_color_depth(COLOUR_DEPTH);
if (set_gfx_mode(fullscreen? GFX_AUTODETECT : GFX_AUTODETECT_WINDOWED, w, h, 0, 0) != 0) {
fprintf(stderr, "Error: %s\n", allegro_error);
return 0;
}
if (install_mouse() < 0) {
fprintf(stderr, "Cannot init. mouse: no driver ?");
return 0;
}
set_mouse_speed(1, 1);
mouse_callback = my_mouse_callback;
keyboard_ucallback = my_keyboard_callback;
sys_event.mx = mouse_x;
sys_event.my = mouse_y;
set_window_title(SIM_TITLE);
return w;
}
void dr_os_close()
{
allegro_exit();
}
/*
* Hier beginnen die eigentlichen graphischen Funktionen
*/
static BITMAP* texture_map;
unsigned short* dr_textur_init(void)
{
texture_map = create_bitmap(width, height);
if (texture_map == NULL) {
printf("Error: can't create double buffer bitmap, aborting!");
exit(1);
}
// return reinterpret_cast<unsigned short*>(texture_map->line[0]);
return (unsigned short *)(texture_map->line[0]);
}
// resizes screen (Not allowed)
int dr_textur_resize(unsigned short**, int, int)
{
display_set_actual_width( width );
return width;
}
void dr_textur(int xp, int yp, int w, int h)
{
blit(texture_map, screen, xp, yp, xp, yp, w, h);
}
/**
* Transform a 24 bit RGB color into the system format.
* @return converted color value
* @author Hj. Malthaner
*/
unsigned int get_system_color(unsigned int r, unsigned int g, unsigned int b)
{
#if 1// USE_16BIT_DIB
return ((r & 0x00F8) << 8) | ((g & 0x00FC) << 3) | (b >> 3);
#else
return ((r & 0x00F8) << 7) | ((g & 0x00F8) << 2) | (b >> 3); // 15 Bit
#endif
}
void dr_prepare_flush()
{
return;
}
void dr_flush(void)
{
display_flush_buffer();
}
/**
* Some wrappers can save screenshots.
* @return 1 on success, 0 if not implemented for a particular wrapper and -1
* in case of error.
* @author Hj. Malthaner
*/
int dr_screenshot(const char *filename)
{
#ifdef WIN32
if(dr_screenshot_png(filename, width-1, height, width, (short unsigned int *)texture_map, 16)) {
return 1;
}
#endif
PALETTE pal;
get_palette(pal);
return save_bitmap(filename, texture_map, pal) == 0 ? 1 : -1;
}
void move_pointer(int x, int y)
{
position_mouse(x, y);
}
void set_pointer(int loading)
{
// not supported
}
static inline int recalc_keys(void)
{
int mod = key_shifts;
return
(mod & KB_SHIFT_FLAG ? 1 : 0) |
(mod & KB_CTRL_FLAG ? 2 : 0);
}
static void internalGetEvents(void)
{
if (event_top_mark != event_bot_mark) {
sys_event.type = event_queue[event_bot_mark++];
sys_event.code = event_queue[event_bot_mark++];
sys_event.mx = event_queue[event_bot_mark++];
sys_event.my = event_queue[event_bot_mark++];
sys_event.mb = event_queue[event_bot_mark++];
event_bot_mark++; // jump over mouse_z
sys_event.key_mod = recalc_keys();
if (event_bot_mark >= queue_length * queue_items) {
event_bot_mark = 0;
}
} else {
sys_event.type = SIM_NOEVENT;
sys_event.code = SIM_NOEVENT;
sys_event.mx = mouse_x;
sys_event.my = mouse_y;
sys_event.mb = mouse_b;
}
}
void GetEvents(void)
{
while (event_top_mark == event_bot_mark) {
// try to be nice where possible
rest(1);
}
do {
internalGetEvents();
} while(sys_event.type == SIM_MOUSE_MOVE);
}
void GetEventsNoWait(void)
{
if (event_top_mark != event_bot_mark) {
do {
internalGetEvents();
} while (event_top_mark != event_bot_mark && sys_event.type == SIM_MOUSE_MOVE);
} else {
sys_event.type = SIM_NOEVENT;
sys_event.code = SIM_NOEVENT;
sys_event.key_mod = recalc_keys();
sys_event.mx = mouse_x;
sys_event.my = mouse_y;
}
}
void show_pointer(int yesno)
{
}
void ex_ord_update_mx_my(void)
{
sys_event.mx = mouse_x;
sys_event.my = mouse_y;
sys_event.mb = mouse_b;
}
static volatile long milli_counter;
void timer_callback(void)
{
milli_counter += 5;
}
END_OF_FUNCTION(timer_callback)
static void simtimer_init(void)
{
printf("Installing timer...\n");
LOCK_VARIABLE(milli_counter);
LOCK_FUNCTION(timer_callback);
printf("Preparing timer ...\n");
install_timer();
printf("Starting timer...\n");
if (install_int(timer_callback, 5) == 0) {
printf("Timer installed.\n");
}
else {
dr_fatal_notify("Error: Timer not available, aborting.\n");
exit(1);
}
}
unsigned long dr_time(void)
{
return milli_counter;
}
void dr_sleep(uint32 usec)
{
rest(usec);
}
int main(int argc, char **argv)
{
return sysmain(argc, argv);
}
END_OF_MAIN()
|