游戏开发论坛

 找回密码
 立即注册
搜索
查看: 3539|回复: 1

光线引擎教程,使用image做按钮

[复制链接]

11

主题

37

帖子

37

积分

注册会员

Rank: 2

积分
37
发表于 2009-2-14 12:04:00 | 显示全部楼层 |阅读模式
因为我的vista系统和cegui不兼容无奈只好用image控件做按钮咯
不做不知道一做吓一跳,制作方法虽然有点繁琐但是要比cegui好理解很多.
我的运行环境是 vista 64位系统 vs2005 vs2008
首先创建win32空项目一个然后配置 包含 附加库
然后创建一个头文件 stdafx.h 在其中输入

  1. #define _WIN32_WINNT 0x4000
  2. // Windows 头文件:
  3. #include <windows.h>
  4. #include <Winuser.h>
  5. //#include "resource.h" //如果你没有增加资源这个就不需要添加了
  6. #include "stdio.h"
  7. #include "include/light.h"
  8. #pragma comment(lib,"light.lib")
  9. #include "include/helper.h"
复制代码

然后创建程序主文件gui_test.cpp

  1. //引入stdafx.h头文件
  2. #include "stdafx.h"

  3. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
  4. {
  5.         LightEngine *engine;
  6.         LightImage *image[9];
  7.         LIMAGEPLUS plus;
  8.         LightInput  *input;
  9.         LightText   *text;
  10.         LightSound  *sound;
  11.         WNDCLASSEX   wcl;   //结构体变量将被用作定义窗口类
  12.         POINT point, last;
  13.         last.x = last.y = 0;

  14.         //初始化光线引擎
  15.         engine = InitialLightEngine();
  16.         // 初始化输入设备
  17.         input = engine->NewInput();
  18.         text = engine->NewText();
  19.         sound = engine->NewSound();
  20.         image[0] = engine->NewImage();
  21.         image[1] = engine->NewImage();
  22.         image[2] = engine->NewImage();
  23.         image[3] = engine->NewImage();
  24.         image[4] = engine->NewImage();
  25.         image[5] = engine->NewImage();
  26.         image[6] = engine->NewImage();
  27.         image[7] = engine->NewImage();
  28.         image[8] = engine->NewImage();

  29.         wcl.hIcon   =   LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1));   /*   标准图标   */  
  30.     wcl.hIconSm   =   LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1));   /*   小图标   */      
  31.         ShowCursor( FALSE ); //取消默认鼠标显示

  32.         //设置显示模式为窗口模式
  33.         engine->SetString(LIGHT_WINDOW_CAPTION,"游戏GUI测试");
  34.         //设置窗口模式
  35.         engine->SetWindowMode(200, 120, 800, 600, 0);
  36.         //设置显示模式为全屏模式 宽度 高度 色宽 后面2个不知道什么
  37.         //engine->SetDisplayMode( 800,600,32,0,0 );

  38.         //开启垂直同步(系统默认)
  39.         //engine->Enable( LIGHT_SWAP_INTERVAL );
  40.         //关闭垂直同步
  41.         //engine->Disable( LIGHT_SWAP_INTERVAL );
  42.         //禁用关键色 (系统默认)
  43.         //engine->Disable( LIGHT_COLOR_KEY );
  44.         //启用关键色
  45.         engine->Enable( LIGHT_COLOR_KEY );
  46.         //设置关键色(透明色,默认值为黑色 0,0,0 )
  47.         //engine->SetColorKey( 255, 255, 255 );

  48.         // 将图像文件读入内存
  49.         image[0]->Load("../data/img/gui_bj01.jpg");
  50.         image[1]->Load("../data/img/bt_1.png");
  51.         image[2]->Load("../data/img/bt_0.png");
  52.         image[3]->Load("../data/img/bt_s_1.png");
  53.         image[4]->Load("../data/img/bt_s_0.png");
  54.         image[5]->Load("../data/img/m1.png");
  55.         image[6]->Load("../data/img/m2.png");
  56.         image[7]->Load("../data/img/m1.png"); //未使用
  57.         image[8]->Load("../data/img/m1.png"); //未使用

  58.         //打开 MP3 文件
  59.         sound->Load("../data/sound/bs1.wav");
  60.         //sound->Load("../data/sound/2pop01.wav");
  61.         sound->SetRepeats(LIGHT_REPEAT_INFINITE);
  62.         sound->Play();

  63.         bool guishow=true;
  64.         int but_type=0;
  65.         do
  66.         {
  67.                 // 清除屏幕内容
  68.                 engine->Clear( LIGHT_COLOR_BUFFER_BIT | LIGHT_DEPTH_BUFFER_BIT );

  69.                 //将光标位置输入到point变量中.
  70.                 input->GetCursorPos(&point);
  71.                 if(guishow==true)
  72.                 {
  73.                         //显示背景图像
  74.                         ZeroMemory( &plus, sizeof(plus) );
  75.                         plus.dest_rect.left = 0;
  76.                         plus.dest_rect.top = 0;
  77.                         plus.dest_rect.right = 800;
  78.                         plus.dest_rect.bottom = 600;
  79.                         plus.flags = LFDESTRECT;
  80.                         image[0]->DrawPlus(0,0,&plus);

  81.                         //显示单人游戏按钮
  82.                         point.x>=50&&point.x<=206&&point.y>=120&&point.y<=151?image[2]->Draw(50,120):image[1]->Draw(50,120);
  83.                         text->Color( 255, 255, 255 );
  84.                         text->SetTextPos( 105, 128 );
  85.                         text->DrawText("单人游戏");

  86.                         //显示多人游戏按钮
  87.                         point.x>=50&&point.x<=206&&point.y>=165&&point.y<=196?image[2]->Draw(50,165):image[1]->Draw(50,165);
  88.                         text->Color( 255, 255, 255 );
  89.                         text->SetTextPos( 105, 173 );
  90.                         text->DrawText("多人游戏");

  91.                         //现实选项按钮
  92.                         point.x>=50&&point.x<=206&&point.y>=210&&point.y<=241?image[2]->Draw(50,210):image[1]->Draw(50,210);
  93.                         text->Color( 255, 255, 255 );
  94.                         text->SetTextPos( 117, 218 );
  95.                         text->DrawText("选项");

  96.                         //显示退出游戏按钮
  97.                         point.x>=50&&point.x<=206&&point.y>=255&&point.y<=286?image[2]->Draw(50,255):image[1]->Draw(50,255);
  98.                         text->Color( 255, 255, 255 );
  99.                         text->SetTextPos( 105, 263 );
  100.                         text->DrawText("退出游戏");

  101.                         //事件
  102.                         switch(but_type)
  103.                         {
  104.                                 case 1:
  105.                                         //显示游戏难度按钮图像
  106.                                         ZeroMemory( &plus, sizeof(plus) );
  107.                                         plus.dest_rect.left = 250;
  108.                                         plus.dest_rect.top = 160;
  109.                                         plus.dest_rect.right = 400;
  110.                                         plus.dest_rect.bottom = 197;
  111.                                         plus.flags = LFDESTRECT;
  112.                                         point.x>=250&&point.x<=400&&point.y>=160&&point.y<=197?image[4]->DrawPlus(0,0,&plus):image[3]->DrawPlus(0,0,&plus);
  113.                                         text->Color( 255, 255, 255 );
  114.                                         text->SetTextPos( 315, 172 );
  115.                                         text->DrawText("初级");

  116.                                         ZeroMemory( &plus, sizeof(plus) );
  117.                                         plus.dest_rect.left = 430;
  118.                                         plus.dest_rect.top = 160;
  119.                                         plus.dest_rect.right = 580;
  120.                                         plus.dest_rect.bottom = 197;
  121.                                         plus.flags = LFDESTRECT;
  122.                                         point.x>=430&&point.x<=580&&point.y>=160&&point.y<=197?image[4]->DrawPlus(0,0,&plus):image[3]->DrawPlus(0,0,&plus);
  123.                                         text->Color( 255, 255, 255 );
  124.                                         text->SetTextPos( 495, 172 );
  125.                                         text->DrawText("中级");

  126.                                         ZeroMemory( &plus, sizeof(plus) );
  127.                                         plus.dest_rect.left = 610;
  128.                                         plus.dest_rect.top = 160;
  129.                                         plus.dest_rect.right = 760;
  130.                                         plus.dest_rect.bottom = 197;
  131.                                         plus.flags = LFDESTRECT;
  132.                                         point.x>=610&&point.x<=760&&point.y>=160&&point.y<=197?image[4]->DrawPlus(0,0,&plus):image[3]->DrawPlus(0,0,&plus);
  133.                                         text->Color( 255, 255, 255 );
  134.                                         text->SetTextPos( 675, 172 );
  135.                                         text->DrawText("高级");
  136.                                 break;
  137.                                 case 2:
  138.                                 break;
  139.                                 case 3:
  140.                                 break;
  141.                         };

  142.                         //触发按钮事件
  143.                         //当点击单人游戏时
  144.                         if(input->GetKeyState(KEY_MOUSE_BUTTON0)&&point.x>=50&&point.x<=206&&point.y>=120&&point.y<=151)
  145.                         {
  146.                                 but_type=1;
  147.                         }
  148.                         //当点击多人游戏时
  149.                         if(input->GetKeyState(KEY_MOUSE_BUTTON0)&&point.x>=50&&point.x<=206&&point.y>=165&&point.y<=196)
  150.                         {
  151.                                 but_type=2;
  152.                         }
  153.                         //当点击选项时
  154.                         if(input->GetKeyState(KEY_MOUSE_BUTTON0)&&point.x>=50&&point.x<=206&&point.y>=210&&point.y<=241)
  155.                         {
  156.                                 but_type=3;
  157.                         }
  158.                         //当点击退出游戏按钮时退出游戏
  159.                         if(input->GetKeyState(KEY_MOUSE_BUTTON0)&&point.x>=50&&point.x<=206&&point.y>=255&&point.y<=286)
  160.                         {
  161.                                 goto append;
  162.                         }

  163.                         //显示鼠标,因为光线引擎没有设置图片的层次,最后绘制的图片在上  所以把鼠标放到最后
  164.                         input->GetKeyState(KEY_MOUSE_BUTTON0)?image[6]->Draw(point.x,point.y):image[5]->Draw(point.x,point.y);
  165.                 }
  166.                
  167.                 //显示帧速(FPS)
  168.                 char fps[20];
  169.                 sprintf_s( fps,20, "帧速: %d 帧/秒", engine->GetFramePerSecond() );
  170.                 text->Color( 0, 255, 0 );
  171.                 text->SetTextPos( 30, 550 );
  172.                 text->DrawText( fps );

  173.                 //显示音乐播放状态
  174.                 char vc[18];
  175.                 sprintf_s( vc,18, "音乐播放状态:%s",sound->GetState()==2?"播放":"停止");
  176.                 text->Color( 0, 255, 0 );
  177.                 text->SetTextPos( 180, 550 );
  178.                 text->DrawText( vc );

  179.                 //显示光标位置
  180.                 char m_xy[30];
  181.                 sprintf_s( m_xy,30, "鼠标位置 X轴:%d Y轴:%d",point.x, point.y);
  182.                 text->Color( 0, 255, 0 );
  183.                 text->SetTextPos( 300, 550 );
  184.                 text->DrawText( m_xy );

  185.                 //显示退出提示
  186.                 text->Color( 0, 255, 0 );
  187.                 text->SetTextPos( 480, 550 );
  188.                 text->DrawText("按ESC退出程序");


  189.                 if(input->GetKeyState(KEY_ESCAPE))goto append;
  190.                 if(input->GetKey()==KEY_P)sound->GetState()==0?sound->Play():sound->Stop();
  191.                
  192.                 // 更新显示
  193.                 engine->SwapBuffers();
  194.         }
  195.         // 配送消息
  196.         while(engine->DispatchMessage());
  197. append:

  198.         sound->Stop();
  199.         // 从内存中释放图像文件
  200.         image[0]->Unload();
  201.         image[1]->Unload();
  202.         image[2]->Unload();
  203.         image[3]->Unload();
  204.         image[4]->Unload();
  205.         image[5]->Unload();
  206.         image[6]->Unload();
  207.         image[7]->Unload();
  208.         image[8]->Unload();

  209.         return 0;
  210. }
复制代码

特奉献出来给大家共享 哈哈
我的网站www.5hku.com 有空来踩踩 [em20]

27

主题

409

帖子

440

积分

中级会员

Rank: 3Rank: 3

积分
440
QQ
发表于 2009-2-18 11:15:00 | 显示全部楼层

Re:光线引擎教程,使用image做按钮

效果不错!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-7-2 06:12

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表