SDL入门教程(五):7、鼠标事件演示,代码重用
2012-09-10 10:15
573 查看
作者:龙飞
7.1:演示程序源代码
今天因为一个网上的朋友的请求,做个一个关于鼠标事件的演示程序。实际上,可以完全用到前面我们构造的类和类方法,这里送上主程序,供大家参考。其他两个文件和图片文件均不需要任何改变。
#include "SurfaceClass.h"
int game(int argc, char* argv[]);
int main(int argc ,char* argv[])
{
int mainRtn = 0;
try {
mainRtn = game(argc, argv);
}
catch ( const ErrorInfo& info ) {
info.show();
return -1;
}
return mainRtn;
}
int game(int argc ,char* argv[])
{
//Create a SDL screen.
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
ScreenSurface screen(SCREEN_WIDTH, SCREEN_HEIGHT);
//Create 2 SDL surface for the screen that just created.
DisplaySurface backGround("bg.bmp", screen);
DisplaySurface frontImage("image.bmp", screen);
//Blit backGround surface to screen and flip the screen.
backGround.blit();
screen.flip();
//moving image's coordinate.
int xpos = 0;
int ypos = 0;
//mouse's coordinate.
int px = 0;
int py = 0;
//moving image's size.
const int IMG_WIDTH = 128;
const int IMG_HEIGHT = 128;
//main loop.
bool gameOver = false;
while( gameOver == false ){
//press ESC or click X to quit.
SDL_Event gameEvent;
while ( SDL_PollEvent(&gameEvent) != 0 ){
if ( gameEvent.type == SDL_QUIT ){
gameOver = true;
}
if ( gameEvent.type == SDL_KEYUP ){
if ( gameEvent.key.keysym.sym == SDLK_ESCAPE ){
gameOver = true;
}
}
//mouse event
if ( gameEvent.type == SDL_MOUSEMOTION ) {
px = gameEvent.motion.x;
py = gameEvent.motion.y;
}
}
if ( xpos < px )
xpos++;
if ( xpos > px )
xpos--;
if ( ypos < py )
ypos++;
if ( ypos > py )
ypos--;
backGround.blit(xpos, ypos, xpos, ypos, IMG_WIDTH, IMG_HEIGHT);
frontImage.blit(xpos, ypos);
screen.flip();
}
return 0;
}
此篇文章来自【/article/10941192.html】
7.1:演示程序源代码
今天因为一个网上的朋友的请求,做个一个关于鼠标事件的演示程序。实际上,可以完全用到前面我们构造的类和类方法,这里送上主程序,供大家参考。其他两个文件和图片文件均不需要任何改变。
#include "SurfaceClass.h"
int game(int argc, char* argv[]);
int main(int argc ,char* argv[])
{
int mainRtn = 0;
try {
mainRtn = game(argc, argv);
}
catch ( const ErrorInfo& info ) {
info.show();
return -1;
}
return mainRtn;
}
int game(int argc ,char* argv[])
{
//Create a SDL screen.
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
ScreenSurface screen(SCREEN_WIDTH, SCREEN_HEIGHT);
//Create 2 SDL surface for the screen that just created.
DisplaySurface backGround("bg.bmp", screen);
DisplaySurface frontImage("image.bmp", screen);
//Blit backGround surface to screen and flip the screen.
backGround.blit();
screen.flip();
//moving image's coordinate.
int xpos = 0;
int ypos = 0;
//mouse's coordinate.
int px = 0;
int py = 0;
//moving image's size.
const int IMG_WIDTH = 128;
const int IMG_HEIGHT = 128;
//main loop.
bool gameOver = false;
while( gameOver == false ){
//press ESC or click X to quit.
SDL_Event gameEvent;
while ( SDL_PollEvent(&gameEvent) != 0 ){
if ( gameEvent.type == SDL_QUIT ){
gameOver = true;
}
if ( gameEvent.type == SDL_KEYUP ){
if ( gameEvent.key.keysym.sym == SDLK_ESCAPE ){
gameOver = true;
}
}
//mouse event
if ( gameEvent.type == SDL_MOUSEMOTION ) {
px = gameEvent.motion.x;
py = gameEvent.motion.y;
}
}
if ( xpos < px )
xpos++;
if ( xpos > px )
xpos--;
if ( ypos < py )
ypos++;
if ( ypos > py )
ypos--;
backGround.blit(xpos, ypos, xpos, ypos, IMG_WIDTH, IMG_HEIGHT);
frontImage.blit(xpos, ypos);
screen.flip();
}
return 0;
}
此篇文章来自【/article/10941192.html】
相关文章推荐
- SDL入门教程(五):7、鼠标事件演示,代码重用
- 一段有用的java3d代码,演示了java3d鼠标滑过对象事件。
- SDL入门教程(九):5、文本反馈鼠标位置坐标信息
- AsWing入门教程 2.2 鼠标事件
- SDL入门教程(九):2、显示文本的完整代码
- SDL入门教程(五):6、对C++异常机制的思考,代码重写
- SDL入门教程(五):6、对C++异常机制的思考,代码重写
- SDL入门教程(九):5、文本反馈鼠标位置坐标信息
- Ajax PHP简单入门教程代码
- SDL入门教程(四):2、SDL动画的硬件渲染(Hardware Render)
- 鼠标右击事件代码(asp.net后台)
- vs2005入门 .Net2.0视频教程 之 DataSet 演示[视频]
- SDL入门教程(十三):1、多线程,从动画说起
- 【配套代码 及 视频下载地址】 使用Visual Studio2005入门.Net2.0系列视频教程
- input text默认值鼠标事件textarea默认值的鼠标事件代码
- jQuery快速入门基础教程之事件(二)
- js鼠标、键盘事件实例代码
- 9.Django入门:高级教程-如何编写可重用的应用
- opencv2+ 鼠标事件详解及示例代码