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 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
|
/*
* This file is part of the MLV Library.
*
* Copyright (C) 2010,2011,2012,2016 Adrien Boussicault, Marc Zipstein
*
*
* This Library is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This Library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this Library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "MLV_window.h"
#include "MLV_color.h"
#include "MLV_shape.h"
#include "config.h"
#include "text.h"
#include "window.h"
#include "input_box.h"
#include "MLV_color.h"
#include "MLV_audio.h"
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "platform.h"
#ifndef MEMORY_DEBUG
#if defined( OS_WINDOWS )
# include <SDL/SDL.h>
# include <SDL/SDL_gfxPrimitives.h>
#elif defined( OS_APPLE )
# include <SDL/SDL.h>
# include <SDL/SDL_gfxPrimitives.h>
#else
# include <SDL/SDL.h>
# include <SDL/SDL_gfxPrimitives.h>
#endif
#else
#include "memory_debug.h"
#endif
#include "warning_error.h"
#include "data_structure.h"
#include "image.h"
#include "memory_management.h"
#define SIZE_DEFAULT_FONT 12
int mlv_sdl_is_initialised = 0;
Uint32 mlv_width_of_desktop = 0;
Uint32 mlv_height_of_desktop = 0;
DataMLV *MLV_data = NULL;
void (*MLV_call_back_function_for_exit)(void*) = NULL;
void *MLV_call_back_data = NULL;
int events_filter(const SDL_Event *event){
if( event->type == SDL_QUIT ){
if( MLV_call_back_function_for_exit ){
(*(MLV_call_back_function_for_exit))(MLV_call_back_data);
return 1;
}else{
exit(0);
}
}
return input_box_events_filter(event);
}
/**************************************************************************/
/* Initialisation and closing functions for default font */
/**************************************************************************/
void initialize_default_font(
const char* path_to_font, unsigned int size_font
){
if( !MLV_data ){
ERROR("The MLV library has not been yet initialised. To initialise the \
MLV Library, create a new window with the MLV_create_window function."
);
}
MLV_data->defaultFont = MLV_load_font( path_to_font, size_font );
}
void free_default_font(){
if( !MLV_data ){
ERROR("The MLV library has not been yet initialised. To initialise the \
MLV Library, create a new window with the MLV_create_window function."
);
}
MLV_free_font( MLV_data->defaultFont );
}
void intialize_post_production_images(){
MLV_data->post_screen = MLV_data->screen;
MLV_data->post_production_image = MLV_create_image(
MLV_data->width, MLV_data->height
);
MLV_data->screen = create_surface( MLV_data->width, MLV_data->height );
}
void free_post_production_images(){
SDL_FreeSurface( MLV_data->screen );
MLV_free_image( MLV_data->post_production_image );
MLV_data->post_production_image = NULL;
MLV_data->screen = MLV_data->post_screen;
MLV_data->post_screen = NULL;
}
/**************************************************************************/
/* Creation d'un fenetre graphique */
/**************************************************************************/
void initialise_graphic_window(
unsigned int width, unsigned int height, int full_screen_is_required
){
int we_need_to_reinitialize_post_screen = 0;
if( MLV_data->post_screen ){
free_post_production_images();
we_need_to_reinitialize_post_screen = 1;
}
#if defined( OS_ANDROID )
Uint32 video_mode_flags = SDL_SWSURFACE | SDL_SRCALPHA;
#else
Uint32 video_mode_flags = SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_SRCALPHA;
#endif
if( full_screen_is_required ){
video_mode_flags |= SDL_FULLSCREEN;
}
MLV_data->full_screen_is_enable = full_screen_is_required;
if( MLV_data->screen ){
SDL_FreeSurface( MLV_data->screen );
}
MLV_data->screen = SDL_SetVideoMode( width, height, MLV_BPP, video_mode_flags );
width = MLV_data->screen->w;
height = MLV_data->screen->h;
MLV_data->width = width;
MLV_data->height = height;
MLV_data->rectangle.x = 0;
MLV_data->rectangle.y = 0;
MLV_data->rectangle.w = width;
MLV_data->rectangle.h = height;
if( MLV_data->save_screen ){
SDL_FreeSurface( MLV_data->save_screen );
}
MLV_data->save_screen = create_surface( width, height ); // do we have to use SDL_ALPHA_OPAQUE here ?
if ( MLV_data->screen == NULL ){
fprintf(stderr,"Unable to set %dx%d video: %s\n", width, height, SDL_GetError());
exit(1);
}
/**************************************************************************/
/* Initialisation de la couleur du fond de la fenetre en noir. */
/**************************************************************************/
MLV_data->ground_color = MLV_COLOR_BLACK;
MLV_draw_filled_rectangle(0,0,width,height, MLV_data->ground_color);
MLV_actualise_window();
if( we_need_to_reinitialize_post_screen ){
intialize_post_production_images();
}
}
void initialise_sdl(){
if( ! mlv_sdl_is_initialised ){
if(
SDL_Init(
SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_AUDIO
#if defined( OS_LINUX ) // SDL thread is not supported on Windows and MacOSX
| SDL_INIT_EVENTTHREAD // Cette option est importante !
// Elle permet de faire en sorte
// que le filtrage des évenements soit fait dés
// réception d'un evenement de la part de l'OS
// Si cette option n'est pas activée, le filtre
// d'evenement est applique seulement après un
// appel à poll_event. Dans ce cas la gestion
// de l'arret automatique du programme après
// utilisation de la croix des fenetre ne
// fonctione plus correctement. En effet, si
// l'utilisateur n'utilise pas de fonction mettant
// en jeu des évènement, le filtre n'est jamais
// appelé et le mécanisme d'arret automatique
// non plus.
#endif
) <0
){
printf("Unable to init SDL: %s\n", SDL_GetError());
exit(1);
}
}
}
void initialise_size_of_desktop(){
initialise_sdl();
if( mlv_width_of_desktop==0 || mlv_height_of_desktop==0 ){
const SDL_VideoInfo* info = SDL_GetVideoInfo();
mlv_width_of_desktop = info->current_w;
mlv_height_of_desktop = info->current_h;
}
}
void MLV_register_a_post_producter( void ( *post_producter )( MLV_Image* ) ){
if( ! MLV_data->post_screen ){
intialize_post_production_images();
}
MLV_data->post_producters = MLV_prepend_list(
MLV_data->post_producters,
(void*) post_producter
);
}
void MLV_unregister_a_post_producter( void ( *post_producter )( MLV_Image* ) ){
assert( MLV_data->post_producters );
MLV_data->post_producters = MLV_remove_list(
MLV_data->post_producters, (void*) post_producter
);
if( ! MLV_data->post_producters ){
free_post_production_images();
}
}
void unregister_all_producters(){
if( MLV_data->post_producters ){
MLV_free_list( MLV_data->post_producters );
free_post_production_images();
}
}
void init_post_producter_infrastructure(){
MLV_data->post_screen = NULL;
MLV_data->post_producters = NULL;
MLV_data->post_production_image = NULL;
}
void MLV_create_window_with_default_font(
const char* window_name, const char* icone_name,
unsigned int width, unsigned int height,
const char* path_to_font, unsigned int size_font
){
if( MLV_data ){
ERROR("The MLV library has yet been initialised.");
}
/**************************************************************************/
/* Initialisation de la strcutre de MLV */
/**************************************************************************/
MLV_data = MLV_MALLOC( 1, DataMLV );
MLV_data->screen = NULL;
MLV_data->save_screen = NULL;
MLV_data->leonardo = NULL;
init_post_producter_infrastructure();
/**************************************************************************/
/* Initialisation de la libriaire SDL pour l'uilisation de la video et du */
/* temps */
/**************************************************************************/
initialise_sdl();
/**************************************************************************/
/* Save the size of the desktop */
/**************************************************************************/
initialise_size_of_desktop();
/**************************************************************************/
/* Creation d'un fenetre graphique */
/**************************************************************************/
initialise_graphic_window( width, height, 0 );
/**************************************************************************/
/* Configuration des noms de la fenetre */
/**************************************************************************/
SDL_WM_SetCaption(window_name, icone_name);
/**************************************************************************/
/* Configuration du mechanisme permettant d'utiliser les boîtes de saisie */
/**************************************************************************/
init_input_box_mechanism();
/**************************************************************************/
/* Configuration des fonts */
/**************************************************************************/
init_font();
/**************************************************************************/
/* Initialisation de tortue Leonardo, la mascotte de MLV */
/**************************************************************************/
init_leonardo_turtle();
/**************************************************************************/
/* Création d'une font par default */
/**************************************************************************/
initialize_default_font( path_to_font, size_font );
/**************************************************************************/
/* Initialise the Framerate Manager */
/**************************************************************************/
SDL_initFramerate( &(MLV_data->frame_rate_manager) );
SDL_setFramerate( &(MLV_data->frame_rate_manager), 100 );
/**************************************************************************/
/* Initialise the internal Framerate Manager for MLV_wait_event */
/**************************************************************************/
SDL_initFramerate( &(MLV_data->frame_rate_manager_for_MLV_wait_event) );
SDL_setFramerate( &(MLV_data->frame_rate_manager_for_MLV_wait_event), 24 );
SDL_SetEventFilter(events_filter);
}
void MLV_create_window(
const char* window_name, const char* icone_name,
unsigned int width, unsigned int height
){
MLV_create_window_with_default_font(
window_name, icone_name, width, height,
DATA_PATH "/font/DejaVuSerif-Bold.ttf" , SIZE_DEFAULT_FONT
);
}
void MLV_change_default_font(
const char* path_to_font, unsigned int size_font
){
free_default_font();
initialize_default_font( path_to_font, size_font );
}
void MLV_enable_full_screen(){
initialise_graphic_window( MLV_data->width, MLV_data->height, 1 );
}
void MLV_create_full_screen_window(
const char* window_name, const char* icone_name,
unsigned int width, unsigned int height
){
MLV_create_window( window_name, icone_name, width, height );
MLV_enable_full_screen();
}
void MLV_create_full_screen_window_with_default_font(
const char* window_name, const char* icone_name,
unsigned int width, unsigned int height,
const char* path_to_font, unsigned int size_font
){
MLV_create_window_with_default_font(
window_name, icone_name, width, height, path_to_font, size_font
);
MLV_enable_full_screen();
}
void MLV_disable_full_screen(){
initialise_graphic_window( MLV_data->width, MLV_data->height, 0 );
}
int MLV_is_full_screen(){
return MLV_data->full_screen_is_enable;
}
void MLV_change_window_size( unsigned int with, unsigned int height ){
initialise_graphic_window( with, height, MLV_data->full_screen_is_enable );
}
void MLV_change_window_caption(
const char* window_name, const char* icone_name
){
SDL_WM_SetCaption( window_name, icone_name );
}
void MLV_get_window_size( unsigned int* width, unsigned int* height ){
*width = MLV_data->width;
*height = MLV_data->height;
}
int MLV_get_window_height(){
return MLV_data->height;
}
int MLV_get_window_width(){
return MLV_data->width;
}
void MLV_clear_window( MLV_Color color ){
MLV_draw_filled_rectangle(
0, 0, MLV_get_window_width( ), MLV_get_window_height( ), color
);
}
void MLV_free_window(){
if( ! MLV_data ){
ERROR("No window has been created.");
}
free_leonardo_turtle();
unregister_all_producters();
SDL_FreeSurface(MLV_data->screen);
SDL_FreeSurface(MLV_data->save_screen);
free_default_font();
quit_font();
MLV_FREE( MLV_data, DataMLV );
MLV_data = NULL;
SDL_Quit();
quit_input_box_mechanism();
}
void draw_producter( void* data, void* user_data ){
void ( *post_producter )( MLV_Image* ) = (void (*)( MLV_Image* )) data;
MLV_Image* image = (MLV_Image*) user_data;
post_producter( image );
}
void prepare_post_production_image(){
MLV_draw_filled_rectangle_on_image(
0, 0, MLV_get_window_width( ), MLV_get_window_height( ),
MLV_data->ground_color,
MLV_data->post_production_image
);
MLV_set_alpha_on_image(
MLV_ALPHA_TRANSPARENT, MLV_data->post_production_image
);
MLV_foreach_list(
MLV_data->post_producters, draw_producter,
MLV_data->post_production_image
);
}
void MLV_update_window(){
if( (! MLV_data ) || (! MLV_data->screen) ){
ERROR("A window can't be displayed whitout being created.");
}
if( ! MLV_data->post_producters ){
SDL_Flip(MLV_data->screen);
}else{
prepare_post_production_image();
boxColor(
MLV_data->post_screen, 0, 0, MLV_data->width-1, MLV_data->height-1,
MLV_data->ground_color
);
SDL_BlitSurface(
MLV_data->screen, NULL, MLV_data->post_screen, &MLV_data->rectangle
);
SDL_BlitSurface(
MLV_data->post_production_image->surface, NULL,
MLV_data->post_screen, &MLV_data->rectangle
);
SDL_Flip(MLV_data->post_screen);
}
}
#ifndef OS_APPLE // Hack to compile with MAC OS 10.9 (maverick)
inline
#endif
void MLV_actualise_window(){
MLV_update_window();
}
void MLV_execute_at_exit( void (*function)(void*), void* data ){
if( MLV_data ){
ERROR("The mlv window is yet initialized. The function execute_at_exit() must be executed before the create_window() function.");
}
if( MLV_call_back_function_for_exit ){
ERROR("The call back function for exiting program is yet initialized.");
}
MLV_call_back_function_for_exit = function;
MLV_call_back_data = data;
}
void MLV_get_desktop_size( unsigned int* width, unsigned int* height ){
initialise_size_of_desktop();
*width = mlv_width_of_desktop;
*height = mlv_height_of_desktop;
}
int MLV_get_desktop_height( ){
initialise_size_of_desktop();
return mlv_height_of_desktop;
}
int MLV_get_desktop_width( ){
initialise_size_of_desktop();
return mlv_width_of_desktop;
}
|