按键上的SDL递增计数器

时间:2014-07-27 20:22:07

标签: c++ menu sdl counter

所以我正在学习SDL,而我正在尝试制作基本菜单。菜单包括"开始",在它下面"加载"在它下面"退出"。他们都没有功能,他们只是为了表演。

我正在做的是添加一个围绕所选选项的框。我试着这样做,无论何时按下键盘上下,计数器将增加或减少一个,盒子将向上或向下移动。但是,每次按下其中一个按钮,计数器就会变大。我知道,因为我在屏幕上显示计数器的代码旁边写了。

如果我做错了,我真的很感激。 截图:http://i.imgur.com/6Io8cLj.png

PS:我使用带有函数apply_surface()的头文件,所以我先添加它。

开始使用head.h:

#ifndef CYOP_H_HEADER
#define CYOP_H_HEADER
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_ttf.h"

using namespace std;

void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination)
{
SDL_Rect offset;
offset.x = x;
offset.y = y;

Uint32 colorkey = SDL_MapRGB( source->format, 0, 0xFF, 0xFF );
SDL_SetColorKey(source, SDL_SRCCOLORKEY, colorkey);

SDL_BlitSurface(source, NULL, destination, &offset);
}

#endif

main.cpp的开始:

#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_ttf.h"
#include "head.h"
#include <string>
#include <sstream>

using namespace std;

// Text rendering parts
TTF_Font *font = NULL;
SDL_Color color = {255, 0, 0};

int cur=0; // Current Menu Item (0 is start, 1 is Load, 2 is Quit)

string str = "Counter: ";
string num;
string strc = str+num;

bool quitp = false; //Quit Program bool
ostringstream convert;

SDL_Surface* background = NULL;
SDL_Surface* start = NULL;
SDL_Surface* load = NULL;
SDL_Surface* quit = NULL;
SDL_Surface* screen = NULL;
SDL_Surface* select = NULL;
SDL_Surface* message = NULL;

SDL_Event event;

void selected(int n)
/*Function that is supposed to reload all images again,
 only the "select" picture at a different place**/
{
convert << cur;
num=convert.str();
strc=str+num;

message = TTF_RenderText_Solid(font, strc.c_str(), color); // Renders "Counter: *number
of cur **/

int arr[3]={50, 100, 150};
apply_surface(0,0,background,screen);
apply_surface(60,50,start,screen);
apply_surface(60,100,load,screen);
apply_surface(60,150,quit,screen);
apply_surface(0, 0, message, screen);
apply_surface(60, arr[n], select, screen);
SDL_Flip(screen);
}

int main(int argc, char* args[])
{
SDL_Init(SDL_INIT_EVERYTHING);
TTF_Init();

screen = SDL_SetVideoMode(300, 300, 32, SDL_SWSURFACE);
font = TTF_OpenFont("grasping.ttf", 28);
message = TTF_RenderText_Solid(font, strc.c_str(), color);

background = IMG_Load ("background.bmp");
start = IMG_Load ("start1.bmp");
load = IMG_Load ("load1.bmp");
quit = IMG_Load ("quit1.bmp");
select = IMG_Load ("select.bmp");

apply_surface(0,0,background,screen);
apply_surface(60,50,start,screen);
apply_surface(60,100,load,screen);
apply_surface(60,150,quit,screen);
apply_surface(60, 50, select, screen);
apply_surface(0, 0, message, screen);

SDL_Flip(screen);

while(quitp==false)
{
    if(SDL_PollEvent(&event));
    {
         if (event.type == SDL_QUIT)
         {
        quitp = true;
         }

        if (event.type == SDL_KEYDOWN)
         {
        SDLKey keyPressed = event.key.keysym.sym;

        switch (keyPressed)
        {
              case SDLK_DOWN:
                  cur++;
                  break;
              case SDLK_UP:
                  cur--;
                  break;
              default: break;

        }selected(cur);

          }// Closing if(event.type == SDL_KEYDOWN)
    }// Closing if(SDL_PollEvent)
} // Closing main while loop


SDL_Quit();

return 0;
}

1 个答案:

答案 0 :(得分:1)

有两个问题:

第一个是经典的菜鸟错误:

if(SDL_PollEvent(&event));

你在这里要做的是:

if(SDL_PollEvent(&event))

令人惊讶的是,一个角色有什么不同!

第二个问题是:

  

但是,每次按下其中一个按钮,计数器就会进入   数量巨大。我知道,因为我写的代码与显示   屏幕上的计数器。

您需要仔细查看该截图。它打印出的数字是123456789101112.看那里的模式?

  • cur = 1,strc =“Counter:1”
  • cur = 2,strc =“Counter:12”
  • cur = 3,strc =“Counter:123”
  • cur = 4,strc =“Counter:1234”
  • ..等

您使用字符串流的方式每次都会附加数字。这里简单的解决方案是每次都不使用相同的字符串流,或者将其重置为空字符串。