2011-04-01 125 views
3

我想让我的游戏沿着y轴和x轴作为玩家滚动。屏幕滚动游戏

当前窗口的坐标系已经失效。 :(

我使用在OpenGL下面一行滚动:

float tempX=px1; 
float tempY=py1; 
double left = tempX-screenWidth/2; 
double right = screenWidth/2+tempX; 
double bottom = tempY - screenWidth/2 + 300; 
double top = screenHeight/2+tempY+ 300; 
//gluOrtho2D(0,screenWidth,0,screenHeight); (before scroll screen) 
gluOrtho2D(left, right, bottom , top); 

PX1和PY1涉及到我的球员,并更新了球员动作 目前,如果你得到的窗口,并调整其大小,而游戏运行时你可以看到图像调整大小和我的代码是错误的,因为正方形显示为矩形

+0

当窗口调整大小或者显示更多相同大小的地图时,您是否希望一切都可以放大? – genpfault 2011-04-01 20:44:09

+1

要显示更多相同大小的地图,所以我可以右转看地图的其余部分并跳到平台上,以便屏幕上升 – himmerz 2011-04-01 20:48:19

回答

3

这样的事情?

#include <GL/glut.h> 

#include <map> 
using namespace std; 

size_t win_w = 0; 
size_t win_h = 0; 

void Box(int xoff, int yoff, int size = 10) 
{ 
    glBegin(GL_QUADS); 
     glVertex2f(xoff + -1*size, yoff + -1*size); 
     glVertex2f(xoff + 1*size, yoff + -1*size); 
     glVertex2f(xoff + 1*size, yoff + 1*size); 
     glVertex2f(xoff + -1*size, yoff + 1*size); 
    glEnd();  
} 

map< int, bool > key_map; 

void keyboard(int key, int x, int y) 
{ 
    key_map[key] = true; 
} 

void keyboard_up(int key, int x, int y) 
{ 
    key_map[key] = false; 
} 


void display(void) 
{ 
    glEnable(GL_DEPTH_TEST); 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

    static int center_x = 0; 
    static int center_y = 0; 
    if(key_map[GLUT_KEY_LEFT] ) center_x--; 
    if(key_map[GLUT_KEY_RIGHT]) center_x++; 
    if(key_map[GLUT_KEY_DOWN] ) center_y--; 
    if(key_map[GLUT_KEY_UP] ) center_y++; 

    double left = center_x - win_w/2.0; 
    double right = center_x + win_w/2.0; 
    double bottom = center_y - win_h/2.0; 
    double top = center_y + win_h/2.0; 

    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    glOrtho(left, right, bottom, top, -10, 10); 

    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 

    glColor3ub(255,0,0); 
    Box(10, 10); 

    glColor3ub(0,255,0); 
    Box(100, 100); 

    glutSwapBuffers(); 
} 

void reshape(int w, int h) 
{ 
    win_w = w; 
    win_h = h; 
    glViewport(0, 0, w, h); 
} 

void idle() 
{ 
    glutPostRedisplay(); 
} 


int main(int argc, char **argv) 
{ 
    glutInit(&argc, argv); 
    glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE); 

    glutInitWindowSize(200,200); 
    glutCreateWindow("Scroll"); 

    glutDisplayFunc(display); 
    glutReshapeFunc(reshape); 
    glutSpecialFunc(keyboard); 
    glutSpecialUpFunc(keyboard_up); 

    glutIdleFunc(idle); 
    glutMainLoop(); 
    return 0; 
} 
+1

这个答案如何不同于我所说的?我只是没有写出整个代码! :) – Ali1S232 2011-04-01 21:14:04

+1

我的上帝是如何爱这些独角兽。很难不投票给别人看看他们! – manneorama 2011-04-01 21:27:54

+0

开始工作并了解这种效果的最佳示例。谢谢。 – 2012-11-19 21:59:12

4

我想问题是与视口分辨率,我的意思是当你调整窗口的大小时,你的视口保持不变像以前一样,你仍然在绘制例如640x480的屏幕,同时显示你在800x600中绘制的内容分辨率,所以我认为你必须在任何窗口大小调整事件后致电glViewPort(0,0,screenWidth,screenHeight)

+0

是的,我每次更新都会调用它, – himmerz 2011-04-01 20:59:29

+0

glOrtho(tempX - screenWidth/2 ,tempX + screenWidth/2,tempY - screenHeight/2,tempY + screenHeight/2,-1,1); \t //也许这会工作? – himmerz 2011-04-01 21:00:13

+1

所以你确定你正在更新screenWidth和screenHeight?和'glveiwPort'不是每个帧调用的东西,你只需要调用它,每当你从Windows窗口调整大小的事件 – Ali1S232 2011-04-01 21:03:50