SDL c++: if函数不工作.编译精细-逻辑错误



大家好,

我的if函数不工作。这是一个逻辑错误。有人能发现这个错误吗?我花了很长时间查看代码……此外,任何改进我的格式/我放置变量的地方都会很酷。

逻辑错误都是基于bgtoggleif语句由注释指出。

提前感谢!

卢卡

main.cpp:

#include "SDL.h"
#include "SDL_opengl.h"
#include <iostream>
void drawBox( int xpos , int ypos , int bwidth , int bheight );
void drawCrossHair();
int width = 400;
int height = 800;
bool bgtoggle = false;
int main(int argc, char* args[])
{
  SDL_Init(SDL_INIT_EVERYTHING);
  SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
  SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8 );
  SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
  SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, 8 );
  SDL_GL_SetAttribute( SDL_GL_BUFFER_SIZE, 32 );
  SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
  SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
  SDL_WM_SetCaption( "Our First Game" , NULL );
  SDL_SetVideoMode( width , height, 32 , SDL_OPENGL );
  glClearColor( 1 , 1 , 1, 1 );
  glViewport( 0,0 , width,height );
  glShadeModel( GL_SMOOTH );
  glMatrixMode( GL_PROJECTION );
  glLoadIdentity();
  glDisable(GL_DEPTH_TEST);
  bool isRunning = true;
  SDL_Event event;
  while ( isRunning )
  {
    while ( SDL_PollEvent( &event ) )
    {
      if ( event.type == SDL_QUIT )
            isRunning = false;
      if ( event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_ESCAPE )
            isRunning = false;
    //THIS IF STATEMANT
      if ( bgtoggle == false && event.type == SDL_KEYUP && event.key.keysym.sym ==  SDLK_b )
      {
            glClearColor( 0 , 0 , 1 , 1 );
            bgtoggle = true;
      }
      // AND THIS IF STATEMENT
      if ( bgtoggle == true && event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_b)           {
        {
            glClearColor( 1 , 1 , 1 , 1 );
            bgtoggle = false;
        }
      }
      glClear( GL_COLOR_BUFFER_BIT );
      glPushMatrix();
      glOrtho( 0 , width , height , 0 , -1 , 1 );
      glBegin(GL_QUADS);
        glColor4ub( 255 , 80 , 80 , 255 );
        glVertex2f( 5 , 5 );
        glVertex2f( width - 5 , 5 );
        glColor4ub( 0 , 0 , 255 , 255 );
        glVertex2f( width - 5 , height - 5 );
        glVertex2f( 5 , height - 5 );
      glEnd();
      drawCrossHair();
      glPopMatrix();
      SDL_GL_SwapBuffers();
    }
    SDL_Quit();
    return 0;
}
void drawCrossHair()
{
 glBegin( GL_LINES );
        glColor4ub( 0 , 0 , 0 , 255); 
        glVertex2f( width / 2 - 5 , height / 2 );
        glVertex2f( width / 2 - 15 , height / 2 );
        glVertex2f( width / 2 + 5, height / 2 );
        glVertex2f( width / 2 + 15 , height / 2 );
        glVertex2f( width / 2 , height / 2 - 5 );
        glVertex2f( width / 2 , height / 2 - 15 );
        glVertex2f( width / 2 , height / 2 + 5 );
        glVertex2f( width / 2 , height / 2 + 15 );
    glEnd();
    glBegin( GL_QUADS );
        glVertex2f( width / 2 - 1 , height / 2 + 1 );
        glVertex2f( width / 2 + 1 , height / 2 + 1 );
        glVertex2f( width / 2 + 1 , height / 2 - 1 );
        glVertex2f( width / 2 - 1 , height / 2 - 1 );
    glEnd();
    glBegin( GL_POINTS );
        glVertex2f( width / 2 , height / 2 );
    glEnd();
}

这很简单。在您的第一个if中,您将bgtoggle设置为true。这使得第二个if的条件为真,从而再次将bgtoggle设置为假。

你最好这样做:

  if (event.type == SDL_KEYUP && event.key.keysym.sym ==  SDLK_b )
        bgtoggle = ! bgtoggle;
  if(bgtoggle)
        glClearColor( 0 , 0 , 1 , 1 );
  else
        glClearColor( 1 , 1 , 1 , 1 );

相关内容

最新更新