📄 sdlguitest.cpp
字号:
// ============================================================================
// Data Structures For Game Programmers
// Ron Penton
// SDLGUITest.h
// Simple test frame for the SDLGUI library
// ============================================================================
// ============================================================================
#include "SDLGUI.h"
#include <string.h>
// ============================================================================
// Global Constants
// ============================================================================
const char PROGRAM_NAME[] = "INSERT PROGRAM NAME HERE";
const int WIDTH = 640;
const int HEIGHT = 480;
const int ITEMS = 32;
// ============================================================================
// Global Variables
// ============================================================================
SDLGUI* g_gui;
char g_string1[128] = "";
char g_string2[128] = "";
void ButtonFunc()
{
if( g_gui->GetItem( 0 )->Visible() )
g_gui->GetItem( 0 )->SetVisibility( false );
else
g_gui->GetItem( 0 )->SetVisibility( true );
}
void CheckFunc()
{
static bool check = false;
check = !check;
if( check == true )
g_gui->GetItem( 1 )->SetVisibility( true );
else
g_gui->GetItem( 1 )->SetVisibility( false );
}
void Text1Func()
{
strcpy( g_string2, g_string1 );
}
void Text2Func()
{
strcpy( g_string1, g_string2 );
}
int main( int argc, char* argv[] )
{
// declare coordinates.
int x, y;
// declare event holder
SDL_Event event;
// set our at exit function
atexit( SDL_Quit );
//initialize systems
SDL_Init( SDL_INIT_VIDEO | SDL_INIT_TIMER );
TTF_Init();
// create the GUI and set the caption.
g_gui = new SDLGUI( WIDTH, HEIGHT, ITEMS, WHITE );
SDL_WM_SetCaption( PROGRAM_NAME, 0);
// add your GUI items here.
g_gui->SetFont( "ARIAL.TTF", 0, 18, TTF_STYLE_NORMAL );
g_gui->AddLabel( 0, 0, "Testing, one two three!", 0, BLACK, WHITE );
g_gui->AddLabel( 320, 0, "MOOOO!", 0, BLUE, WHITE );
g_gui->GetItem( 1 )->SetVisibility( false );
g_gui->AddButton( 20, 50, "gbup.bmp", "gbdown.bmp",
"Test Button!", 0, BLACK, WHITE, ButtonFunc );
g_gui->AddCheckbox( 320, 50, "checkboxup.bmp", "checkboxdown.bmp",
"CLICKY!", 0, RED, WHITE, CheckFunc );
g_gui->AddTextBox( 10, 150, 256, TTF_FontHeight(g_gui->GetFont(0)),
g_string1, 128, 0, BLACK, WHITE, Text1Func );
g_gui->AddTextBox( 320, 150, 128, TTF_FontHeight(g_gui->GetFont(0)),
g_string2, 128, 0, GREEN, WHITE, Text2Func );
// main message loop.
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 );
// tell the GUI that a button has been pressed
g_gui->MouseDown( x, y );
}
if( event.type == SDL_MOUSEBUTTONUP )
{
// get the mouse state.
SDL_GetMouseState( &x, &y );
// tell the GUI that a button has been released
g_gui->MouseUp( 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 );
}
// tell the GUI that a key was pressed.
g_gui->KeyDown( event.key.keysym.sym, event.key.keysym.mod );
}
} // end event loop.
// do all game-related stuff here.
// tell the GUI to redraw itself
g_gui->Draw();
// DO ALL YOUR RENDERING HERE
// tell the GUI to update itself
g_gui->Update();
}
// done
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -