?? g07-01.cpp
字號:
// ============================================================================
// G07-01.cpp
// Stack-based Menu Demo
// ============================================================================
#include "SDL.h"
#include "SDLHelpers.h"
#include "Stack.h"
#include <stdlib.h>
// ============================================================================
// Global Constants
// ============================================================================
const char PROGRAM_NAME[] = "Game Demo 07-01: Menu Demo";
const int WIDTH = 800;
const int HEIGHT = 600;
const int ICONS = 8;
// ============================================================================
// Classes
// ============================================================================
class Menu
{
public:
char* m_options[3]; // the text that describes the option
int m_optionSpawns[3]; // the menu index that the option leads to
int m_x; // x position
int m_y; // y position
int m_w; // width
int m_h; // height
SDL_Color m_color; // color
};
// ============================================================================
// Global Variables
// ============================================================================
// this is the main window for the framework
SDL_Surface* g_window = 0;
// the font
TTF_Font* g_font;
// the menus
Array<Menu> g_menus( 10 );
// the menu stack, at most 3 levels deep.
AStack<Menu*> g_stack( 3 );
// ============================================================================
// Functions
// ============================================================================
void DrawMenu( SDL_Surface* p_surface, Menu* p_menu )
{
// draw the background of the menu
SDLBox( p_surface, p_menu->m_x, p_menu->m_y,
p_menu->m_w, p_menu->m_h, p_menu->m_color );
// draw the border
SDLLine( p_surface, p_menu->m_x, p_menu->m_y,
p_menu->m_x + p_menu->m_w, p_menu->m_y, BLACK );
SDLLine( p_surface, p_menu->m_x, p_menu->m_y + p_menu->m_h,
p_menu->m_x + p_menu->m_w, p_menu->m_y + p_menu->m_h, BLACK );
SDLLine( p_surface, p_menu->m_x, p_menu->m_y,
p_menu->m_x, p_menu->m_y + p_menu->m_h, BLACK );
SDLLine( p_surface, p_menu->m_x + p_menu->m_w, p_menu->m_y,
p_menu->m_x + p_menu->m_w, p_menu->m_y + p_menu->m_h, BLACK );
// now draw the text.
int i;
int y = p_menu->m_y + 16;
SDL_Surface* text;
for( i = 0; i < 3; i++ )
{
if( p_menu->m_options[i] != 0 )
{
text = TTF_RenderText_Shaded( g_font, p_menu->m_options[i],
BLACK, p_menu->m_color );
SDLBlit( text, p_surface, p_menu->m_x + 16, y );
SDL_FreeSurface( text );
y += TTF_FontHeight( g_font );
}
}
}
int main( int argc, char* argv[] )
{
// declare coordinates.
int x, y;
// declare event holder
SDL_Event event;
// create the menu boxes
// main menu
g_menus[0].m_options[0] = "1 - Sound";
g_menus[0].m_optionSpawns[0] = 1;
g_menus[0].m_options[1] = "2 - Graphics";
g_menus[0].m_optionSpawns[1] = 2;
g_menus[0].m_options[2] = "3 - Controls";
g_menus[0].m_optionSpawns[2] = 3;
g_menus[0].m_x = 16;
g_menus[0].m_y = 16;
g_menus[0].m_w = 768;
g_menus[0].m_h = 568;
g_menus[0].m_color = LTGREY;
// Sound menu
g_menus[1].m_options[0] = "1 - FX Volume";
g_menus[1].m_optionSpawns[0] = 4;
g_menus[1].m_options[1] = "2 - Music Volume";
g_menus[1].m_optionSpawns[1] = 5;
g_menus[1].m_options[2] = 0;
g_menus[1].m_optionSpawns[2] = 0;
g_menus[1].m_x = 100;
g_menus[1].m_y = 100;
g_menus[1].m_w = 384;
g_menus[1].m_h = 384;
g_menus[1].m_color = RED;
// Graphics menu
g_menus[2].m_options[0] = "1 - Graphic Violence Controls";
g_menus[2].m_optionSpawns[0] = 6;
g_menus[2].m_options[1] = "2 - Detail Controls";
g_menus[2].m_optionSpawns[1] = 7;
g_menus[2].m_options[2] = 0;
g_menus[2].m_optionSpawns[2] = 0;
g_menus[2].m_x = 100;
g_menus[2].m_y = 100;
g_menus[2].m_w = 384;
g_menus[2].m_h = 384;
g_menus[2].m_color = BLUE;
// Controls menu
g_menus[3].m_options[0] = "1 - Mouse Controls";
g_menus[3].m_optionSpawns[0] = 8;
g_menus[3].m_options[1] = "2 - Keyboard Controls";
g_menus[3].m_optionSpawns[1] = 9;
g_menus[3].m_options[2] = 0;
g_menus[3].m_optionSpawns[2] = 0;
g_menus[3].m_x = 100;
g_menus[3].m_y = 100;
g_menus[3].m_w = 384;
g_menus[3].m_h = 384;
g_menus[3].m_color = GREEN;
// Sound FX menu
g_menus[4].m_options[0] = "Common buddy, I know you want those";
g_menus[4].m_optionSpawns[0] = 0;
g_menus[4].m_options[1] = "SFX SUPER LOUD!";
g_menus[4].m_optionSpawns[1] = 0;
g_menus[4].m_options[2] = 0;
g_menus[4].m_optionSpawns[2] = 0;
g_menus[4].m_x = 50;
g_menus[4].m_y = 250;
g_menus[4].m_w = 512;
g_menus[4].m_h = 200;
g_menus[4].m_color = BLUE;
// Music menu
g_menus[5].m_options[0] = "Turn up the Volume of the Deathmatch Music!";
g_menus[5].m_optionSpawns[0] = 0;
g_menus[5].m_options[1] = 0;
g_menus[5].m_optionSpawns[1] = 0;
g_menus[5].m_options[2] = 0;
g_menus[5].m_optionSpawns[2] = 0;
g_menus[5].m_x = 200;
g_menus[5].m_y = 250;
g_menus[5].m_w = 512;
g_menus[5].m_h = 200;
g_menus[5].m_color = ORANGE;
// Violence menu
g_menus[6].m_options[0] = "Turn on the blood and the gore!";
g_menus[6].m_optionSpawns[0] = 0;
g_menus[6].m_options[1] = 0;
g_menus[6].m_optionSpawns[1] = 0;
g_menus[6].m_options[2] = 0;
g_menus[6].m_optionSpawns[2] = 0;
g_menus[6].m_x = 200;
g_menus[6].m_y = 350;
g_menus[6].m_w = 512;
g_menus[6].m_h = 200;
g_menus[6].m_color = GREEN;
// Detail menu
g_menus[7].m_options[0] = "Got a Geforce5?";
g_menus[7].m_optionSpawns[0] = 0;
g_menus[7].m_options[1] = "Turn up the details!";
g_menus[7].m_optionSpawns[1] = 0;
g_menus[7].m_options[2] = 0;
g_menus[7].m_optionSpawns[2] = 0;
g_menus[7].m_x = 50;
g_menus[7].m_y = 300;
g_menus[7].m_w = 512;
g_menus[7].m_h = 200;
g_menus[7].m_color = YELLOW;
// Mouse Controls menu
g_menus[8].m_options[0] = "Plug that optical mouse in and";
g_menus[8].m_optionSpawns[0] = 0;
g_menus[8].m_options[1] = "turn up the sensitivity!";
g_menus[8].m_optionSpawns[1] = 0;
g_menus[8].m_options[2] = 0;
g_menus[8].m_optionSpawns[2] = 0;
g_menus[8].m_x = 150;
g_menus[8].m_y = 250;
g_menus[8].m_w = 384;
g_menus[8].m_h = 200;
g_menus[8].m_color = BLUE;
// Keyboard Controls menu
g_menus[9].m_options[0] = "Uhm, I can't think of something witty";
g_menus[9].m_optionSpawns[0] = 0;
g_menus[9].m_options[1] = "to put here... so pretend that I did. =)";
g_menus[9].m_optionSpawns[1] = 0;
g_menus[9].m_options[2] = 0;
g_menus[9].m_optionSpawns[2] = 0;
g_menus[9].m_x = 200;
g_menus[9].m_y = 50;
g_menus[9].m_w = 384;
g_menus[9].m_h = 200;
g_menus[9].m_color = YELLOW;
// push the first menu onto the stack
g_stack.Push( &(g_menus[0]) );
// initialize the video system.
SDL_Init( SDL_INIT_VIDEO | SDL_INIT_TIMER );
SDL_WM_SetCaption( PROGRAM_NAME, 0);
// set our at exit function
atexit( SDL_Quit );
// set the video mode.
g_window = SDL_SetVideoMode( WIDTH, HEIGHT, 0, SDL_ANYFORMAT );
TTF_Init();
g_font = TTF_OpenFont( "arial.ttf", 20 );
TTF_SetFontStyle( g_font, TTF_STYLE_NORMAL );
while( 1 )
{
//look for an event
if( SDL_PollEvent( &event ) )
{
//an event was found
if( event.type == SDL_QUIT )
break;
if( event.type == SDL_MOUSEBUTTONDOWN )
{
// get the mouse state.
SDL_GetMouseState( &x, &y );
}
if( event.type == SDL_MOUSEBUTTONUP )
{
// get the mouse state.
SDL_GetMouseState( &x, &y );
}
if( event.type == SDL_KEYDOWN )
{
// a key was pressed.
if( event.key.keysym.sym == SDLK_ESCAPE )
{
// if ESC was pressed, quit the program.
SDL_Event quit;
quit.type = SDL_QUIT;
SDL_PushEvent( &quit );
}
if( event.key.keysym.sym == SDLK_0 )
{
if( g_stack.Top() != &g_menus[0] )
g_stack.Pop();
}
if( event.key.keysym.sym == SDLK_1 )
{
x = g_stack.Top()->m_optionSpawns[0];
if( x != 0 )
{
g_stack.Push( &g_menus[x] );
}
}
if( event.key.keysym.sym == SDLK_2 )
{
x = g_stack.Top()->m_optionSpawns[1];
if( x != 0 )
{
g_stack.Push( &g_menus[x] );
}
}
if( event.key.keysym.sym == SDLK_3 )
{
x = g_stack.Top()->m_optionSpawns[2];
if( x != 0 )
{
g_stack.Push( &g_menus[x] );
}
}
}
} // end event loop.
// clear the screen to black.
SDL_FillRect( g_window, NULL, SDL_MapRGB( g_window->format, 255, 255, 255 ) );
// draw the stack from the bottom up.
for( x = 0; x < g_stack.Count(); x++ )
{
DrawMenu( g_window, g_stack[x] );
}
// update the entire window.
SDL_UpdateRect( g_window, 0, 0, 0, 0 );
}
// done
return 0;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -