Stackoverflowaria的人们,
我的功能不起作用。这是一个逻辑错误。有人可以发现错误吗?我已经查看了很长时间的代码......还有,任何改进格式的方法/我放置变量的地方都会很酷。
逻辑错误全部基于bgtoggle
。评论指出if
语句。
提前致谢!
卢卡
这是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();
}
答案 0 :(得分:0)
这很简单。在您的第一个if
中,您将bgtoggle
设置为true。这使得第二个if
的条件成立,再次将bgtoggle
设置为false。
你最好这样做:
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 );