Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 击键时的SDL增量计数器_C++_Menu_Sdl_Counter - Fatal编程技术网

C++ 击键时的SDL增量计数器

C++ 击键时的SDL增量计数器,c++,menu,sdl,counter,C++,Menu,Sdl,Counter,所以我在学习SDL,我在尝试做一个基本的菜单。菜单包括“开始”,下面是“加载”,下面是“退出”。它们都没有功能,只是为了作秀 我正在做的是添加一个包围所选选项的框。我试着这样做,每当按下键盘上的“向上”或“向下”键时,计数器将递增或递减1,而方框将向上或向下移动。然而,每次我按下其中一个按钮,计数器就会显示巨大的数字。我知道这一点,因为我在屏幕上显示计数器的代码旁边写了代码 我真的很感激任何对我所做错事的帮助。 截图: PS:我正在使用一个带有函数apply_surface()的头文件,所以我将

所以我在学习SDL,我在尝试做一个基本的菜单。菜单包括“开始”,下面是“加载”,下面是“退出”。它们都没有功能,只是为了作秀

我正在做的是添加一个包围所选选项的框。我试着这样做,每当按下键盘上的“向上”或“向下”键时,计数器将递增或递减1,而方框将向上或向下移动。然而,每次我按下其中一个按钮,计数器就会显示巨大的数字。我知道这一点,因为我在屏幕上显示计数器的代码旁边写了代码

我真的很感激任何对我所做错事的帮助。 截图:

PS:我正在使用一个带有函数apply_surface()的头文件,所以我将首先添加它

头部开始。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;
}
#包括“SDL/SDL.h”
#包括“SDL/SDL_image.h”
#包括“SDL/SDL_ttf.h”
#包括“head.h”
#包括
#包括
使用名称空间std;
//文本呈现部件
TTF_Font*Font=NULL;
SDL_颜色={255,0,0};
int cur=0;//当前菜单项(0为开始,1为加载,2为退出)
string str=“计数器:”;
字符串数;
字符串strc=str+num;
bool-quitp=false//退出程序bool
ostringstream转换;
SDL_表面*背景=空;
SDL_曲面*开始=空;
SDL_表面*load=NULL;
SDL_曲面*quit=NULL;
SDL_表面*屏幕=空;
SDL_曲面*select=NULL;
SDL_表面*信息=NULL;
SDL_事件;
已选择无效(整数n)
/*应该重新加载所有图像的函数,
只有“选择”图片位于不同的位置**/
{

转换有两个问题:

第一个是一个典型的新手错误:

if(SDL_PollEvent(&event));

你想在这里做的是:

if(SDL\u PollEvent(&event))

令人惊讶的是,一个角色能带来多大的不同

第二个问题是:

然而,每次我按下其中一个按钮,计数器就会转到 巨大的数字。我知道这一点,因为我写的代码旁边显示 屏幕上的计数器

你需要更仔细地看一下屏幕截图。它打印出来的数字是12345678910112。看到那里的图案了吗

  • cur=1,strc=“计数器:1”
  • cur=2,strc=“计数器:12”
  • cur=3,strc=“计数器:123”
  • cur=4,strc=“计数器:1234”
  • 等等

您使用字符串流的方式只是每次添加数字。这里的简单解决方案是,要么每次不使用相同的字符串流,要么将其重置为空字符串。

谢谢您的回答,但这不是我遇到的问题。我知道它是1、2、3…问题是,我按下的时间是相同的毫秒,它是g退出屏幕并选择在我按下向下按钮之前,盒子就像老虎机一样移动。我需要它来显示2并移动一个位置。啊,我知道你在问什么。我正在更新答案。:)太感谢你了!我觉得自己太傻了,浪费了5个小时用20多个不同的组合重新编写代码!顺便说一句,计数器显示从来都不是问题因为它永远不会进入程序,我只是写了它,以便在运行程序时看到程序内计数器的情况。请选择您的答案或编辑您的问题。