Ver Mensaje Individual
  #2 (permalink)  
Antiguo 24/05/2015, 22:58
Avatar de vangodp
vangodp
 
Fecha de Ingreso: octubre-2013
Mensajes: 934
Antigüedad: 10 años, 6 meses
Puntos: 38
Respuesta: Unión de dos menús o programas

Código C++:
Ver original
  1. #include <stdio.h>
  2. #include <allegro.h>
  3. #include <cstdlib>
  4. #include <iostream>
  5. #include <time.h>
  6. using namespace std;
  7.  
  8. #define ROJO 0x1AA9D1
  9. #define GRIS 0x333333
  10. #define BLANCO 0xFFFFFF
  11.  
  12.  
  13. inline void iniciarSistemas(int ANCHO, int ALTO){
  14.     allegro_init(); //Inicia la librería Allegro
  15.     install_keyboard(); //Instala el controlador de interrupciones de teclado de Allegro
  16.     set_color_depth(32); //Establece el formato de píxel para ser utilizado por las llamadas posteriores.
  17.     set_gfx_mode(GFX_AUTODETECT_WINDOWED, ANCHO, ALTO, 0, 0); //Pixeles utilizados
  18.     install_mouse(); // Para usar el ratón
  19. }
  20.  
  21.  
  22. int main() {
  23.     iniciarSistemas(800, 531);
  24.    
  25.     BITMAP *buffer = create_bitmap ( 800, 531 );
  26.     BITMAP *cursor = load_bitmap ( "cursor.bmp", NULL );
  27.     //escena1
  28.     BITMAP *escena1Fondo = load_bitmap ( "escena1fondo.bmp", NULL );
  29.     BITMAP *escena1Jugar = load_bitmap ( "escena1jugar.bmp", NULL );
  30.     BITMAP *escena1Salir = load_bitmap ( "escena1salir.bmp", NULL );
  31.     //otras escenas
  32.     BITMAP *menu          = load_bitmap ( "escena2menu.bmp", NULL );
  33.     BITMAP *instrucciones = load_bitmap ( "escena3instrucciones.bmp", NULL );
  34.     BITMAP *creditos      = load_bitmap ( "escena4creditos.bmp", NULL );
  35.  
  36.     if (buffer == NULL || escena1Fondo == NULL || escena1Jugar == NULL || escena1Salir == NULL ||
  37.         menu == NULL || instrucciones == NULL || creditos == NULL || cursor == NULL ) {
  38.         cout << "ERROR al cargar alguna de las imagenes" << endl;
  39.         destroy_bitmap ( buffer );
  40.         destroy_bitmap ( cursor );
  41.         destroy_bitmap ( escena1Fondo );
  42.         destroy_bitmap ( escena1Jugar );
  43.         destroy_bitmap ( escena1Salir );
  44.         destroy_bitmap ( menu );
  45.         destroy_bitmap ( instrucciones);
  46.         destroy_bitmap ( creditos );            
  47.         return 1;
  48.     }
  49.     clear_to_color ( buffer, 0x000000 );
  50.    
  51.     int tiempo_ticks = 0;
  52.     int ticks = CLOCKS_PER_SEC / 20;
  53.     int op = 0;
  54.     int sw = 0;
  55.     int escena = 1; //escena inicial
  56.     bool salida = false;
  57.    
  58.     while ( !salida ) {
  59.         if ( clock() > tiempo_ticks + ticks ) {
  60.             tiempo_ticks = clock();
  61.  
  62.             clear_to_color ( buffer, 0x000000 );
  63.            
  64.             switch ( escena ) {
  65.                 case 1: //escena1. Es la inicial.
  66.                     if ( mouse_x > 176  && mouse_x < 326 && mouse_y > 410 && mouse_y < 485 ) {
  67.                         blit ( escena1Jugar, buffer, 0, 0, 0, 0, 800, 531 ); // Imprime fondo 2 sobre el buffer (0,0,0,0 para que se imprima toda la pantalla)
  68.                        
  69.                         if ( mouse_b & 1 ) { // Si se presiona el botón izquierdo se realiza la acción
  70.                             escena = 2; //****la accion segun entendi es pasar al menu no?****
  71.                         }
  72.                     } else if ( mouse_x > 472 & mouse_x < 619 && mouse_y > 412 & mouse_y < 483 ) {
  73.                         blit ( escena1Salir, buffer, 0, 0, 0, 0, 800, 531 ); // Imprime fondo 3 sobre el buffer (0,0,0,0 para que se imprima toda la pantalla)
  74.                         if ( mouse_b & 1 ) { // Si se presiona el botón izquierdo se realiza la acción
  75.                             salida = true;
  76.                         }
  77.                        
  78.                     } else {
  79.                         blit ( escena1Fondo, buffer, 0, 0, 0, 0, 800, 531 ); // Si no se cumple lo anterior queda el fondo 1
  80.                     }
  81.                     masked_blit ( cursor, buffer, 0, 0, mouse_x, mouse_y, 13, 22 ); //Para colocar transparencia al cursor, se imprime en el buffer con las cordenadas del mouse.
  82.  
  83.                     break;
  84.                 case 2: //escena2
  85.  
  86.                     //Controla el movimiento de arriba abajo del menu
  87.                     if ( !key[KEY_UP] && !key[KEY_DOWN] && sw == 1 ) {sw = 0;}
  88.                     if ( key[KEY_UP] && sw == 0 ) { op--; if ( op == 0 ) op = 5; sw = 1; }
  89.                     if ( key[KEY_DOWN] && sw == 0 ) { op++; if ( op == 6 ) op = 1; sw = 1; }
  90.                    
  91.                     //averigua si se pulsa enter segun en que posicion esteamos en el menu
  92.                     if ( op == 1 && key[KEY_ENTER] ) { escena = 5; }
  93.                     if ( op == 2 && key[KEY_ENTER] ) { escena = 6; }
  94.                     if ( op == 3 && key[KEY_ENTER] ) { escena = 3; }
  95.                     if ( op == 4 && key[KEY_ENTER] ) { escena = 4; }                
  96.                     if ( op == 5 && key[KEY_ENTER] ) { escena = 1; }
  97.                    
  98.                     blit ( menu, buffer, 0, 0, 0, 0, 800, 531 );
  99.                    
  100.                     //Animacion de las letras del menu
  101.                     if ( op == 1 ) { textout_centre_ex ( buffer, font, "Nueva Partida" , 390, 260, ROJO, GRIS );
  102.                     } else {         textout_centre_ex ( buffer, font, "Nueva Partida" , 390, 260, BLANCO, GRIS ); }
  103.                     if ( op == 2 ) { textout_centre_ex ( buffer, font, "Niveles" , 385, 318, ROJO, GRIS );
  104.                     } else {         textout_centre_ex ( buffer, font, "Niveles" , 385, 318, BLANCO, GRIS ); }
  105.                     if ( op == 3 ) { textout_centre_ex ( buffer, font, "Instrucciones" , 390, 368, ROJO, GRIS );
  106.                     } else {         textout_centre_ex ( buffer, font, "Instrucciones" , 390, 368, BLANCO, GRIS ); }
  107.                     if ( op == 4 ) { textout_centre_ex ( buffer, font, "Creditos" , 380, 417, ROJO, GRIS );
  108.                     } else {         textout_centre_ex ( buffer, font, "Creditos" , 380, 417, BLANCO, GRIS ); }
  109.                     if ( op == 5 ) { textout_centre_ex ( buffer, font, "Volver" , 381, 467, ROJO, GRIS );
  110.                     } else {         textout_centre_ex ( buffer, font, "Volver" , 381, 467, BLANCO, GRIS ); }              
  111.                     break;
  112.                 case 3:
  113.                     if ( key[KEY_ESC] ) {escena = 2; }
  114.                     blit ( instrucciones, buffer, 0, 0, 0, 0, 800, 531 );
  115.                     break;
  116.                 case 4:
  117.                     if ( key[KEY_ESC] ) {escena = 2; }
  118.                     blit ( creditos, buffer, 0, 0, 0, 0, 800, 531 );                    
  119.                     break;
  120.                 case 5:
  121.                     if ( key[KEY_ESC] ) {escena = 2; }
  122.                     textout_centre_ex ( buffer, font, "Aqui deberia ir el juego XD" , 390, 260, ROJO, GRIS);
  123.                     break;
  124.                 case 6:
  125.                     if ( key[KEY_ESC] ) {escena = 2; }
  126.                     textout_centre_ex ( buffer, font, "No implementado" , 390, 260, ROJO, GRIS);
  127.                     break;                
  128.                 default:
  129.                     break;
  130.             }
  131.             blit ( buffer, screen, 0, 0, 0, 0, 800, 531 );
  132.         }
  133.    
  134.     }
  135.  
  136.     destroy_bitmap ( buffer );
  137.     destroy_bitmap ( cursor );
  138.     destroy_bitmap ( escena1Fondo );
  139.     destroy_bitmap ( escena1Jugar );
  140.     destroy_bitmap ( escena1Salir );
  141.     destroy_bitmap ( menu );
  142.     destroy_bitmap ( instrucciones);
  143.     destroy_bitmap ( creditos );  
  144.    
  145.     return 0;
  146. }END_OF_MAIN();

Mi foto debería ir junto XDD

Aquí están los archivos ya que las fotos les cambie los nombres porque tenían un raleo del copón que todas se llamaban fondo.
http://dfiles.eu/files/0j5yp6bdg

toma como que cada case del switch es una escena que muestra la foto correcta. La escena 1 esta compuesta por 3 imagenes, pero las demás solo se componen de 1 cada escena.

Es como ir alternando entre escenas con el switch. apenas hice nada, solo las redireccione a que vayan al lugar correcto según el caso.

En fin.. Si no llegas a comprender el concepto de escenas te va resultar dificil. Cada escena como bien puedes ver, tiene su proprio control de teclas, actualización de lógica (no todos, porque algunos menús solo muestran una imagen y no hace falta calcular nada), y por ultimo dibuja su escena.

Quedate que una escena esta compuesta de evento(le tecla), actualiza(cálculos, o lógica), dibuja(blit). Cada caso tiene eso comprueba/actualiza/dibuja. Comprueba teclas, actualiza coordenadas y dibuja según los cambios.¿Capiche?

Todo ese código va muy fullero. deberia ir clases o funciones, acompañados de estructuras etc etc.

Bueno siga aprendiendo C++. Solo así lograrás un buen código. Suerte

Última edición por vangodp; 24/05/2015 a las 23:13