我自己做了一个游戏,就像 Pong 在 C++ 下使用 SDL 和 OpenGL:
#include "SDL.h"
#include "SDL_opengl.h"
#include <iostream>
int main(int argc, char* args[])
{
//initialize SDL
SDL_Init(SDL_INIT_EVERYTHING);
//OpenGL memory
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);
//caption of the window
SDL_WM_SetCaption( "Bine baaa", NULL );
//size
SDL_SetVideoMode(600,400,32, SDL_OPENGL);
//clearcolor
glClearColor(0,0,0,1); //RED,GREEN,BLUE,ALPHA
//portion of screen displayed
glViewport(0,0,600,400);
//for gradients
glShadeModel(GL_SMOOTH);
//2D rendering
glMatrixMode(GL_PROJECTION);
glLoadIdentity();//save
glDisable(GL_DEPTH_TEST);
bool isRunning = true;
SDL_Event event;
typedef struct player{
float myX;
float myY;
float width=15;
float height=60;
bool up=false;
bool down=false;
};
player player1,player2;
player1.myX=10;
player1.myY=160;
player2.myX=580;
player2.myY=160;
float ballX=300;
float ballY=200;
float vitezaX=0.5;
float vitezaY=0.5;
float latura =10;
//main loop
while(isRunning){
//EVENTS
while ( SDL_PollEvent(&event)){
if( event.type == SDL_QUIT )
isRunning=false;
//escape button closes window
if(event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_ESCAPE)
isRunning=false;
if( event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_r)
glClearColor(1,0,0,1);
if( event.type == SDL_KEYDOWN){
if(event.key.keysym.sym==SDLK_UP)
player2.up=true;
if(event.key.keysym.sym==SDLK_DOWN)
player2.down=true;
if(event.key.keysym.sym==SDLK_w)
player1.up=true;
if(event.key.keysym.sym==SDLK_s)
player1.down=true;
}
if( event.type == SDL_KEYUP){
if(event.key.keysym.sym==SDLK_UP)
player2.up=false;
if(event.key.keysym.sym==SDLK_DOWN)
player2.down=false;
if(event.key.keysym.sym==SDLK_w)
player1.up=false;
if(event.key.keysym.sym==SDLK_s)
player1.down=false;
}
}
//LOGIC
if(player1.up==true)
player1.myY-=0.3;
if(player1.down==true)
player1.myY+=0.3;
if(player2.up==true)
player2.myY-=0.3;
if(player2.down==true)
player2.myY+=0.3;
if(ballY<0)
vitezaY=-vitezaY;
if(ballY+latura>400)
vitezaY=-vitezaY;
if(ballX+latura>player2.myX && ballY+latura>player2.myY && ballY<player2.myY+player2.height){
vitezaX=-vitezaX;
if(ballX+latura-player2.myX>=1){
if(vitezaY>0)
ballY=player2.myY-latura;
else
ballY=player2.myY+player2.height;
vitezaX=-vitezaX;
vitezaY=-vitezaY;
}
}
if(ballX<player1.myX+player1.width && ballY+latura>player1.myY && ballY<player1.myY+player1.height){
vitezaX=-vitezaX;
if((player1.myX+player1.width)-ballX>=1){
if(vitezaY>0)
ballY=player1.myY-latura;
else
ballY=player1.myY+player1.height;
vitezaX=-vitezaX;
vitezaY=-vitezaY;
}
}
if(ballX<0 || ballX>600){
ballX=300;
ballY=200;
SDL_Delay(500);
}
ballX+=vitezaX;
ballY+=vitezaY;
//RENDER
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix(); //Begin Render
glColor4ub(255,255,255,255);
glOrtho(0,600,400,0,-1,1);
glBegin(GL_QUADS);//GL_LINES, GL_LINE_STRIP, GL_QUADS, GL_POLIGON,GL_TRIANGLES, GL_LINE_LOOP
glVertex2f(player1.myX,player1.myY);
glVertex2f(player1.myX+player1.width,player1.myY);
glVertex2f(player1.myX+player1.width,player1.myY+player1.height);
glVertex2f(player1.myX,player1.myY+player1.height);
glEnd();//End Draw
glBegin(GL_QUADS);//GL_LINES, GL_LINE_STRIP, GL_QUADS, GL_POLIGON,GL_TRIANGLES, GL_LINE_LOOP
glVertex2f(player2.myX,player2.myY);
glVertex2f(player2.myX+player2.width,player2.myY);
glVertex2f(player2.myX+player2.width,player2.myY+player2.height);
glVertex2f(player2.myX,player2.myY+player2.height);
glEnd();//End Draw
glBegin(GL_QUADS);//GL_LINES, GL_LINE_STRIP, GL_QUADS, GL_POLIGON,GL_TRIANGLES, GL_LINE_LOOP
glVertex2f(ballX,ballY);
glVertex2f(ballX+latura,ballY);
glVertex2f(ballX+latura,ballY+latura);
glVertex2f(ballX,ballY+latura);
glEnd();//End Draw
glPopMatrix(); //End Render
SDL_GL_SwapBuffers();
SDL_Delay(2);
}
SDL_Quit();
return 0;}
注意:“latura”是矩形的宽度或高度,“viteza”是速度。
问题是当我在其他机器上测试游戏时,在我的 PC 上游戏运行非常缓慢,即使我可以说我的 PC 还不错......(2GB RAM,8600GT nvidia 和四核英特尔),在其他机器上游戏的速度要快得多,无论代码是否设置为相同的速度。我似乎无法找到这个问题下的逻辑。我想知道如何让这个游戏在不同的机器上以相同的速度运行(我一直在寻找诸如时间相关动画之类的东西……我不知道会有多大帮助;我还找到了一些关于软件/硬件渲染的主题,这可能是问题吗?在我的 PC 上游戏使用软件渲染而在其他机器上是硬件基础?)。