|
因为我的vista系统和cegui不兼容无奈只好用image控件做按钮咯
不做不知道一做吓一跳,制作方法虽然有点繁琐但是要比cegui好理解很多.
我的运行环境是 vista 64位系统 vs2005 vs2008
首先创建win32空项目一个然后配置 包含 附加库
然后创建一个头文件 stdafx.h 在其中输入
- #define _WIN32_WINNT 0x4000
- // Windows 头文件:
- #include <windows.h>
- #include <Winuser.h>
- //#include "resource.h" //如果你没有增加资源这个就不需要添加了
- #include "stdio.h"
- #include "include/light.h"
- #pragma comment(lib,"light.lib")
- #include "include/helper.h"
复制代码
然后创建程序主文件gui_test.cpp
- //引入stdafx.h头文件
- #include "stdafx.h"
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
- {
- LightEngine *engine;
- LightImage *image[9];
- LIMAGEPLUS plus;
- LightInput *input;
- LightText *text;
- LightSound *sound;
- WNDCLASSEX wcl; //结构体变量将被用作定义窗口类
- POINT point, last;
- last.x = last.y = 0;
- //初始化光线引擎
- engine = InitialLightEngine();
- // 初始化输入设备
- input = engine->NewInput();
- text = engine->NewText();
- sound = engine->NewSound();
- image[0] = engine->NewImage();
- image[1] = engine->NewImage();
- image[2] = engine->NewImage();
- image[3] = engine->NewImage();
- image[4] = engine->NewImage();
- image[5] = engine->NewImage();
- image[6] = engine->NewImage();
- image[7] = engine->NewImage();
- image[8] = engine->NewImage();
- wcl.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1)); /* 标准图标 */
- wcl.hIconSm = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1)); /* 小图标 */
- ShowCursor( FALSE ); //取消默认鼠标显示
-
- //设置显示模式为窗口模式
- engine->SetString(LIGHT_WINDOW_CAPTION,"游戏GUI测试");
- //设置窗口模式
- engine->SetWindowMode(200, 120, 800, 600, 0);
- //设置显示模式为全屏模式 宽度 高度 色宽 后面2个不知道什么
- //engine->SetDisplayMode( 800,600,32,0,0 );
- //开启垂直同步(系统默认)
- //engine->Enable( LIGHT_SWAP_INTERVAL );
- //关闭垂直同步
- //engine->Disable( LIGHT_SWAP_INTERVAL );
- //禁用关键色 (系统默认)
- //engine->Disable( LIGHT_COLOR_KEY );
- //启用关键色
- engine->Enable( LIGHT_COLOR_KEY );
- //设置关键色(透明色,默认值为黑色 0,0,0 )
- //engine->SetColorKey( 255, 255, 255 );
- // 将图像文件读入内存
- image[0]->Load("../data/img/gui_bj01.jpg");
- image[1]->Load("../data/img/bt_1.png");
- image[2]->Load("../data/img/bt_0.png");
- image[3]->Load("../data/img/bt_s_1.png");
- image[4]->Load("../data/img/bt_s_0.png");
- image[5]->Load("../data/img/m1.png");
- image[6]->Load("../data/img/m2.png");
- image[7]->Load("../data/img/m1.png"); //未使用
- image[8]->Load("../data/img/m1.png"); //未使用
- //打开 MP3 文件
- sound->Load("../data/sound/bs1.wav");
- //sound->Load("../data/sound/2pop01.wav");
- sound->SetRepeats(LIGHT_REPEAT_INFINITE);
- sound->Play();
- bool guishow=true;
- int but_type=0;
- do
- {
- // 清除屏幕内容
- engine->Clear( LIGHT_COLOR_BUFFER_BIT | LIGHT_DEPTH_BUFFER_BIT );
- //将光标位置输入到point变量中.
- input->GetCursorPos(&point);
- if(guishow==true)
- {
- //显示背景图像
- ZeroMemory( &plus, sizeof(plus) );
- plus.dest_rect.left = 0;
- plus.dest_rect.top = 0;
- plus.dest_rect.right = 800;
- plus.dest_rect.bottom = 600;
- plus.flags = LFDESTRECT;
- image[0]->DrawPlus(0,0,&plus);
- //显示单人游戏按钮
- point.x>=50&&point.x<=206&&point.y>=120&&point.y<=151?image[2]->Draw(50,120):image[1]->Draw(50,120);
- text->Color( 255, 255, 255 );
- text->SetTextPos( 105, 128 );
- text->DrawText("单人游戏");
- //显示多人游戏按钮
- point.x>=50&&point.x<=206&&point.y>=165&&point.y<=196?image[2]->Draw(50,165):image[1]->Draw(50,165);
- text->Color( 255, 255, 255 );
- text->SetTextPos( 105, 173 );
- text->DrawText("多人游戏");
- //现实选项按钮
- point.x>=50&&point.x<=206&&point.y>=210&&point.y<=241?image[2]->Draw(50,210):image[1]->Draw(50,210);
- text->Color( 255, 255, 255 );
- text->SetTextPos( 117, 218 );
- text->DrawText("选项");
- //显示退出游戏按钮
- point.x>=50&&point.x<=206&&point.y>=255&&point.y<=286?image[2]->Draw(50,255):image[1]->Draw(50,255);
- text->Color( 255, 255, 255 );
- text->SetTextPos( 105, 263 );
- text->DrawText("退出游戏");
- //事件
- switch(but_type)
- {
- case 1:
- //显示游戏难度按钮图像
- ZeroMemory( &plus, sizeof(plus) );
- plus.dest_rect.left = 250;
- plus.dest_rect.top = 160;
- plus.dest_rect.right = 400;
- plus.dest_rect.bottom = 197;
- plus.flags = LFDESTRECT;
- point.x>=250&&point.x<=400&&point.y>=160&&point.y<=197?image[4]->DrawPlus(0,0,&plus):image[3]->DrawPlus(0,0,&plus);
- text->Color( 255, 255, 255 );
- text->SetTextPos( 315, 172 );
- text->DrawText("初级");
- ZeroMemory( &plus, sizeof(plus) );
- plus.dest_rect.left = 430;
- plus.dest_rect.top = 160;
- plus.dest_rect.right = 580;
- plus.dest_rect.bottom = 197;
- plus.flags = LFDESTRECT;
- point.x>=430&&point.x<=580&&point.y>=160&&point.y<=197?image[4]->DrawPlus(0,0,&plus):image[3]->DrawPlus(0,0,&plus);
- text->Color( 255, 255, 255 );
- text->SetTextPos( 495, 172 );
- text->DrawText("中级");
- ZeroMemory( &plus, sizeof(plus) );
- plus.dest_rect.left = 610;
- plus.dest_rect.top = 160;
- plus.dest_rect.right = 760;
- plus.dest_rect.bottom = 197;
- plus.flags = LFDESTRECT;
- point.x>=610&&point.x<=760&&point.y>=160&&point.y<=197?image[4]->DrawPlus(0,0,&plus):image[3]->DrawPlus(0,0,&plus);
- text->Color( 255, 255, 255 );
- text->SetTextPos( 675, 172 );
- text->DrawText("高级");
- break;
- case 2:
- break;
- case 3:
- break;
- };
- //触发按钮事件
- //当点击单人游戏时
- if(input->GetKeyState(KEY_MOUSE_BUTTON0)&&point.x>=50&&point.x<=206&&point.y>=120&&point.y<=151)
- {
- but_type=1;
- }
- //当点击多人游戏时
- if(input->GetKeyState(KEY_MOUSE_BUTTON0)&&point.x>=50&&point.x<=206&&point.y>=165&&point.y<=196)
- {
- but_type=2;
- }
- //当点击选项时
- if(input->GetKeyState(KEY_MOUSE_BUTTON0)&&point.x>=50&&point.x<=206&&point.y>=210&&point.y<=241)
- {
- but_type=3;
- }
- //当点击退出游戏按钮时退出游戏
- if(input->GetKeyState(KEY_MOUSE_BUTTON0)&&point.x>=50&&point.x<=206&&point.y>=255&&point.y<=286)
- {
- goto append;
- }
- //显示鼠标,因为光线引擎没有设置图片的层次,最后绘制的图片在上 所以把鼠标放到最后
- input->GetKeyState(KEY_MOUSE_BUTTON0)?image[6]->Draw(point.x,point.y):image[5]->Draw(point.x,point.y);
- }
-
- //显示帧速(FPS)
- char fps[20];
- sprintf_s( fps,20, "帧速: %d 帧/秒", engine->GetFramePerSecond() );
- text->Color( 0, 255, 0 );
- text->SetTextPos( 30, 550 );
- text->DrawText( fps );
- //显示音乐播放状态
- char vc[18];
- sprintf_s( vc,18, "音乐播放状态:%s",sound->GetState()==2?"播放":"停止");
- text->Color( 0, 255, 0 );
- text->SetTextPos( 180, 550 );
- text->DrawText( vc );
- //显示光标位置
- char m_xy[30];
- sprintf_s( m_xy,30, "鼠标位置 X轴:%d Y轴:%d",point.x, point.y);
- text->Color( 0, 255, 0 );
- text->SetTextPos( 300, 550 );
- text->DrawText( m_xy );
- //显示退出提示
- text->Color( 0, 255, 0 );
- text->SetTextPos( 480, 550 );
- text->DrawText("按ESC退出程序");
- if(input->GetKeyState(KEY_ESCAPE))goto append;
- if(input->GetKey()==KEY_P)sound->GetState()==0?sound->Play():sound->Stop();
-
- // 更新显示
- engine->SwapBuffers();
- }
- // 配送消息
- while(engine->DispatchMessage());
- append:
- sound->Stop();
- // 从内存中释放图像文件
- image[0]->Unload();
- image[1]->Unload();
- image[2]->Unload();
- image[3]->Unload();
- image[4]->Unload();
- image[5]->Unload();
- image[6]->Unload();
- image[7]->Unload();
- image[8]->Unload();
- return 0;
- }
复制代码
特奉献出来给大家共享 哈哈
我的网站www.5hku.com 有空来踩踩 [em20]
 |
|