2011-07-26 1 views
1

J'ai écrit un jeu de casse de briques simple dans OpenGL. La balle est un cercle 2D dessiné en utilisant:Aide Brick Breaker. cercles, pagaies et rebondissements maladroits

for (float angle = 0; angle < (10); angle+=0.01) 
{ 
    glVertex2f((x_pos + sin(angle) * RADIUS), (y_pos + (cos(angle)) * RADIUS)); 
} 

Cela provoque une distorsion lorsque le jeu passe en mode plein écran. RADIUS est défini comme 0,025. J'ai également besoin d'aide pour rendre les mouvements de la palette plus lisses. En outre, si vous jouez plusieurs fois au jeu, vous remarquerez que vers l'extrême gauche, lorsque la balle atteint la pagaie, elle monte jusqu'à une certaine hauteur et rebondit ensuite vers le bas. code complet:

#include <GL/openglut.h> 
#include <stdarg.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <cmath> 
#define RADIUS 0.025 
#define SPEED 0.001 
int WIDTH = 900; 
int HEIGHT = 650; 
int RATIO = WIDTH/HEIGHT; 
bool show[5][10]; 
float x_brick[4][9]; 
float y_brick[4][9]; 
float P_XPOS = 0; 
float P_YPOS = -0.8; 
bool phit_center = false , phit_corner = false; 
bool game_over = false; 
bool RIGHT = 1,LEFT = 0,UP = 1,DOWN = 0; 
bool started = false; 
float x_pos = 0,y_pos = -0.75; 
bool hit = false; 
int lives = 3; 
using namespace std; 
void b_draw() 
{ 
     glColor3f(1.0,0.0,0.0); 
     glBegin(GL_QUADS); 
     for(int a = 0; a < 9; a++) 
     { 
       for(int b = 0; b < 4; b++) 
       { 
         if(show[b][a] == 1) 
         { 
            glVertex2f(x_brick[b][a],y_brick[b][a]); 
            glVertex2f(x_brick[b][a],y_brick[b][a] - 0.10); 
            glVertex2f(x_brick[b][a]+0.2,y_brick[b][a] - 0.10); 
            glVertex2f(x_brick[b][a]+0.2,y_brick[b][a]); 
         } 
       } 
     } 
     glEnd(); 
} 
void c_draw() 
{ 
    glColor3f(0.0,0.0,0.0); 
    glBegin(GL_TRIANGLE_FAN); 
    glVertex2f(x_pos,y_pos); 
    for (float angle = 0; angle < (10); angle+=0.01) 
    { 
     glVertex2f((x_pos + sin(angle) * RADIUS), (y_pos + (cos(angle)) * RADIUS)); 
    } 
    glEnd(); 
} 
bool b_hit() 
{ 
    hit = false; 
    int flag = 1; 
    for(int a = 0; a < 10; a++) 
    { 
      for(int b =0; b < 4; b++) 
      { 
        if(x_pos >= x_brick[b][a] && x_pos <= x_brick[b][a] + 0.2) 
        { 
           if(y_pos <= y_brick[b][a] && y_pos >= y_brick[b][a] - 0.1) 
           { 
             if(show[b][a] == 1) 
             { 
                show[b][a] = 0; 
                flag = 0; 
                hit = true; 
                break; 
             } 
           } 
        } 
      } 
      if(flag == 0) 
        break; 
    } 
    return hit; 
} 
bool crashed() 
{ 
    if(y_pos < P_YPOS - 0.05) 
       return true; 
    else return false;; 
} 
void p_hit() 
{ 
    phit_corner = false; 
    phit_center = false; 
    if(x_pos <= P_XPOS + 0.13 && x_pos >= P_XPOS - 0.13) 
    { 
       if(y_pos <= P_YPOS) 
       { 
         phit_center = true; 
       } 
    } 
    else if((x_pos >= P_XPOS + 0.13 && x_pos <= P_XPOS + 0.2) || (x_pos <= P_XPOS - 0.13 && x_pos >= P_XPOS - 0.2)) 
    { 
       if(y_pos <= P_YPOS) 
       { 
         phit_corner = true; 
       } 
    } 
} 
void c_move() 
{ 
     if(UP && RIGHT) 
     { 
      x_pos += (SPEED); 
      y_pos += (SPEED);  
     } 
     if(UP && LEFT) 
     { 
      x_pos -= (SPEED); 
      y_pos += (SPEED); 
     } 
     if(DOWN && RIGHT) 
     { 
       x_pos += (SPEED); 
       y_pos -= (SPEED); 
     } 
     if(DOWN && LEFT) 
     { 
       x_pos -= (SPEED); 
       y_pos -= (SPEED); 
     } 
     b_hit(); 
     if(x_pos >= (RATIO-RADIUS)) 
     { 
       RIGHT = 0; 
       LEFT = 1; 
     } 
     else if(x_pos <= (-RATIO+RADIUS)) 
     { 
      RIGHT = 1; 
      LEFT = 0; 
     } 
     if(y_pos >= (RATIO-RADIUS) || hit) 
     { 
        UP = 0; 
        DOWN = 1; 
     } 
     else if(y_pos <= (-RATIO+RADIUS) || hit) 
     { 
      UP = 1; 
      DOWN = 0; 
     } 
     p_hit(); 
     if(phit_center) 
     { 
        DOWN = 0; 
        UP = 1; 
     } 
     if(phit_corner) 
     { 
        if(LEFT) 
        { 
          LEFT = 0; 
          RIGHT = 1; 
        } 
        else 
        { 
         RIGHT = 0; 
         LEFT = 1; 
        } 
        UP = 1; 
        DOWN = 0; 
     } 
} 
void p_draw() 
{ 
    glColor3f(0.0,0.0,0.0); 
    glBegin(GL_QUADS); 
         glVertex2f(P_XPOS-0.2,P_YPOS); 
         glVertex2f(P_XPOS+0.2,P_YPOS); 
         glVertex2f(P_XPOS+0.2,P_YPOS-0.05); 
         glVertex2f(P_XPOS-0.2,P_YPOS-0.05); 
    glEnd(); 
} 

void BallLoop() 
{ 
    glClearColor(1.0,1.0,1.0,0); 
    glDisable(GL_DEPTH_TEST); 
    glClear(GL_COLOR_BUFFER_BIT); 
    glMatrixMode (GL_MODELVIEW); 
    glLoadIdentity(); 
    c_draw(); 
    b_draw(); 
    p_draw(); 
    glFlush(); 
    if(started) 
       c_move(); 
    if(crashed()) 
    {   
       x_pos = 0; 
       y_pos = -0.7; 
       started = 0; 
       UP = 1; 
       RIGHT = 1; 
       DOWN = 0; 
       LEFT = 0; 
    } 

    glutSwapBuffers(); 
    glutPostRedisplay(); 
} 
void user_input(unsigned char key, int x, int y) 
{ 
    if(key == 13) 
    started = true; 
} 
void ArrowKeys(int key, int x, int y) 
{ 
    if(key==GLUT_KEY_LEFT && P_XPOS >= -0.8) 
     for(float a = 0; a < 0.05; a+= 0.001) 
     { 
             P_XPOS -=0.003; 
             BallLoop(); 
     } 
    if(key==GLUT_KEY_RIGHT && P_XPOS <= 0.8) 
    { 
          for(float a = 0; a < 0.05; a+= 0.001) 
          { 
             P_XPOS +=0.003; 
             BallLoop(); 
          } 
    } 
} 
void set_xy() 
{ 
    for(int a = 0; a < 5; a++) 
    { 
      for(int b = 0; b < 10; b++) 
      { 
        show[a][b] = 1; 
      } 
    } 
    int c = 0; 
    for(float a = -0.94; c <= 8; a+=0.21) 
    {   

       for(int b = 0; b <= 5; b++) 
       { 
         x_brick[b][c] = a; 

       } 
       c++; 
    } 
    int d = 0; 
    for(float s = 0.99; d <= 3; s-=0.11) 
    { 
       for(int r = 0; r < 9; r++) 
       { 
         y_brick[d][r] = s; 
       } 
       d++; 
    } 
} 
void changeSize(int w, int h) 
{ 

    if(h == 0) 
     h = 1; 
    RATIO = w/h; 
    float ratio = 1.0* w/h; 
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    glViewport(0, 0, w, h); 
    glMatrixMode(GL_MODELVIEW); 
    BallLoop(); 
} 
int main(int argc, char **argv) 
{ 
    set_xy(); 
    glutInit(&argc, argv); 
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA); 
    glutInitWindowPosition(0,0); 
    glutInitWindowSize(WIDTH,HEIGHT);      
    glutCreateWindow("Brick Breaker - By Viraj"); 
    glutReshapeFunc(changeSize); 
    glutDisplayFunc(BallLoop); 
    glutKeyboardFunc(user_input); 
    glutSpecialFunc(ArrowKeys); 
    glutMainLoop(); 
    return 0; 
} 
+1

Pour un mouvement régulier, vérifier cela: http://unity3d.com/support/documentation/ScriptReference/Input.GetAxis.html L'axe a une augmentation exponentielle alors quand vous appuyez sur le bouton vers le bas puis de nouveau tout à coup, votre pagaie ne bouge plus autant. – holgac

Répondre

4

Beaucoup de gens rencontrent ce genre de problème, car ils basent leur code sur des tutoriels mal écrits. Une erreur importante est de faire l'installation de la matrice de projection dans le gestionnaire de resynchronisation. Dans toute application OpenGL sérieuse, y compris les jeux, vous changez la projection plusieurs fois pendant le rendu - pour les HUD, minimaps, éléments GUI, etc.

Vous devez plutôt définir la projection dans le gestionnaire d'affichage, juste avant d'avoir besoin de cette projection. De plus, vous avez manqué de définir la matrice de projection, vous ne définissez que la fenêtre - proche, mais pas assez. Je n'appelle le gestionnaire d'affichage Ball_Loop, mais bien, voici comment je modifier:

void BallLoop() 
{ 
    const int win_width = glutGet(GLUT_WINDOW_WIDTH); 
    const int win_height = glutGet(GLUT_WINDOW_HEIGHT); 
    const float win_aspect = (float)win_width/(float)win_height; 

    glClearColor(1.0,1.0,1.0,0); 
    glDisable(GL_DEPTH_TEST); 
    glClear(GL_COLOR_BUFFER_BIT); 

    glViewport(0, 0, win_width, win_height); 
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    glOrtho(-aspect, aspect, -1, 1, -1, 1); /* those ortho limits should match your game logic */ 

    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 
    c_draw(); 
    b_draw(); 
    p_draw(); 
    glFlush(); 
    if(started) 
       c_move(); 
    if(crashed()) 
    {   
       x_pos = 0; 
       y_pos = -0.7; 
       started = 0; 
       UP = 1; 
       RIGHT = 1; 
       DOWN = 0; 
       LEFT = 0; 
    } 

    glutSwapBuffers(); 
    glutPostRedisplay(); 
} 

EDITcode source de brickbreaker.cc entièrement fixe et jouable

#include <GL/glut.h> 
#include <stdarg.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <cmath> 

using namespace std; 

#define RADIUS 0.025 

#define RATIO (4./3.) 

bool show[5][10]; 
float x_brick[4][9]; 
float y_brick[4][9]; 

float paddle_x = 0; 
float paddle_y = -0.8; 
float paddle_speed = 0; 
const float PaddleSpeedFactor = 3.; 

bool phit_center = false , phit_corner = false; 
bool game_over = false; 

float speed_x = 0.; 
float speed_y = 0.; 

float x_pos = 0,y_pos = -0.75; 
int lives = 3; 

float T_last_frame = 0.; 

void draw_bricks() 
{ 
     glColor3f(1.0,0.0,0.0); 
     glBegin(GL_QUADS); 
     for(int a = 0; a < 9; a++) 
     { 
       for(int b = 0; b < 4; b++) 
       { 
         if(show[b][a] == 1) 
         { 
            glVertex2f(x_brick[b][a],y_brick[b][a]); 
            glVertex2f(x_brick[b][a],y_brick[b][a] - 0.10); 
            glVertex2f(x_brick[b][a]+0.2,y_brick[b][a] - 0.10); 
            glVertex2f(x_brick[b][a]+0.2,y_brick[b][a]); 
         } 
       } 
     } 
     glEnd(); 
} 

void ball_draw() 
{ 
    glColor3f(0.0,0.0,0.0); 
    glBegin(GL_TRIANGLE_FAN); 
    glVertex2f(x_pos,y_pos); 
    for (float angle = 0; angle < (10); angle+=0.01) 
    { 
     glVertex2f((x_pos + sin(angle) * RADIUS), (y_pos + (cos(angle)) * RADIUS)); 
    } 
    glEnd(); 
} 

bool brick_hit() 
{ 
    bool hit = false; 
    int flag = 1; 
    for(int a = 0; a < 10; a++) 
    { 
      for(int b =0; b < 4; b++) 
      { 
        if(x_pos >= x_brick[b][a] && x_pos <= x_brick[b][a] + 0.2) 
        { 
           if(y_pos <= y_brick[b][a] && y_pos >= y_brick[b][a] - 0.1) 
           { 
             if(show[b][a] == 1) 
             { 
                show[b][a] = 0; 
                flag = 0; 
                hit = true; 
                break; 
             } 
           } 
        } 
      } 
      if(flag == 0) 
        break; 
    } 
    return hit; 
} 

bool crashed() 
{ 
    if(y_pos < paddle_y - 0.05) 
       return true; 

    return false; 
} 

void paddle_hit() 
{ 
    phit_corner = false; 
    phit_center = false; 
    if(x_pos <= paddle_x + 0.13 && x_pos >= paddle_x - 0.13) 
    { 
       if(y_pos <= paddle_y) 
       { 
         phit_center = true; 
       } 
    } 
    else if((x_pos >= paddle_x + 0.13 && x_pos <= paddle_x + 0.2) || 
        (x_pos <= paddle_x - 0.13 && x_pos >= paddle_x - 0.2)) 
    { 
       if(y_pos <= paddle_y) 
       { 
         phit_corner = true; 
       } 
    } 
} 

void paddle_move(float dT) 
{ 

    if(paddle_x < RATIO && paddle_x > -RATIO) 
     paddle_x += paddle_speed * PaddleSpeedFactor * dT; 

     if(paddle_x > 0.95) { 
     paddle_x = 0.95; 
     paddle_speed = 0.; 
    } 

     if(paddle_x < -0.95) { 
     paddle_x = -0.95; 
     paddle_speed = 0.; 
    } 

    paddle_speed *= (1. - 0.05); 
    if(fabs(paddle_speed) < 0.01) 
     paddle_speed = 0.; 
} 

void ball_move(float dT) 
{ 
     x_pos += speed_x * dT; 
     y_pos += speed_y * dT; 

     if(brick_hit()) { 
     speed_y *= -1; 
     } 

     if(x_pos >= (RATIO-RADIUS) || x_pos <= (-RATIO+RADIUS)) 
     { 
     speed_x *= -1; 
     } 

     if(y_pos >= (1.-RADIUS)) 
     { 
     speed_y = -1; 
     } 

     paddle_hit(); 
     if(phit_center) 
     { 
     speed_y = 1; 
     } 
     if(phit_corner) 
     { 
     speed_x *= -1; 
     speed_y = 1; 
     } 
} 

void paddle_draw() 
{ 
    glColor3f(0.0,0.0,0.0); 
    glBegin(GL_QUADS); 
         glVertex2f(paddle_x - 0.2, paddle_y); 
         glVertex2f(paddle_x + 0.2, paddle_y); 
         glVertex2f(paddle_x + 0.2, paddle_y - 0.05); 
         glVertex2f(paddle_x - 0.2, paddle_y - 0.05); 
    glEnd(); 
} 

void step_game() 
{ 
    paddle_move(T_last_frame); 
    ball_move(T_last_frame); 

    if(crashed()) 
    { 
    speed_x = 0; 
    speed_y = 0; 
     x_pos = 0; 
     y_pos = -0.7; 
     paddle_speed = 0; 
     paddle_x = 0; 
    } 

    glutPostRedisplay(); 
} 

void launch_ball() 
{ 
    speed_y = 1.; 
    speed_x = 1.; 
} 

void user_input(unsigned char key, int x, int y) 
{ 
    if(key == 13) 
     launch_ball(); 
} 

void ArrowKeys(int key, int x, int y) 
{ 
    if(key==GLUT_KEY_LEFT) 
     paddle_speed = -1.; 

    if(key==GLUT_KEY_RIGHT) 
     paddle_speed = +1.; 
} 

void set_xy() 
{ 
    for(int a = 0; a < 5; a++) 
    { 
      for(int b = 0; b < 10; b++) 
      { 
        show[a][b] = 1; 
      } 
    } 
    int c = 0; 
    for(float a = -0.94; c <= 8; a+=0.21) 
    {   

       for(int b = 0; b <= 5; b++) 
       { 
         x_brick[b][c] = a; 

       } 
       c++; 
    } 
    int d = 0; 
    for(float s = 0.99; d <= 3; s-=0.11) 
    { 
       for(int r = 0; r < 9; r++) 
       { 
         y_brick[d][r] = s; 
       } 
       d++; 
    } 
} 

void display() 
{ 
    const int win_width = glutGet(GLUT_WINDOW_WIDTH); 
    const int win_height = glutGet(GLUT_WINDOW_HEIGHT); 
    const float win_aspect = (float)win_width/(float)win_height; 

    glViewport(0, 0, win_width, win_height); 

    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    if(win_aspect > RATIO) { 
     glOrtho(-win_aspect, win_aspect, -1., 1., -1., 1.); 
    } else { 
     glOrtho(-RATIO, RATIO, -RATIO/win_aspect, RATIO/win_aspect, -1., 1.); 
    } 

    glMatrixMode(GL_MODELVIEW); 

    glClearColor(0., 0., 1., 1.); 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

     glBegin(GL_QUADS); 
     glColor3f(1,1,1); 
     glVertex2f(-RATIO, -1); 
     glVertex2f(RATIO, -1); 
     glVertex2f(RATIO, 1); 
     glVertex2f(-RATIO, 1); 
     glEnd(); 

    draw_bricks(); 
    paddle_draw(); 
    ball_draw(); 

    glutSwapBuffers(); 

     // GLUT doesn't offer cross plattform timing 
     // assume 60Hz refresh rate 
     T_last_frame = 1./60.; 
} 

int main(int argc, char **argv) 
{ 
    set_xy(); 

    glutInit(&argc, argv); 
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA); 

    glutInitWindowPosition(0,0); 
    glutInitWindowSize(800, 600); 

    glutCreateWindow("Brick Breaker - By Viraj"); 
    glutDisplayFunc(display); 

    glutKeyboardFunc(user_input); 
    glutSpecialFunc(ArrowKeys); 

    glutIdleFunc(step_game); 

    glutMainLoop(); 

    return 0; 
} 

EDIT 2 et juste pour l'exhaustivité, voici une version GLFW

#include <GL/glfw.h> 

#include <stdlib.h> 
#include <math.h> 

using namespace std; 

#define RADIUS 0.025 

#define RATIO (4./3.) 

bool show[5][10]; 
float x_brick[4][9]; 
float y_brick[4][9]; 

const float SpeedFactor = 10.; 

float paddle_x = 0; 
float paddle_y = -0.8; 
float paddle_speed = 0; 
const float PaddleSpeedFactor = 3.; 

bool phit_center = false, phit_corner = false; 
bool game_over = false; 

float speed_x = 0.; 
float speed_y = 0.; 

float x_pos; 
float y_pos; 
int lifes = 0; 

void draw_bricks() 
{ 
    glColor3f(1.0, 0.0, 0.0); 
    glBegin(GL_QUADS); 
    for (int a = 0; a < 9; a++) { 
     for (int b = 0; b < 4; b++) { 
      if (show[b][a] == 1) { 
       glVertex2f(x_brick[b][a], y_brick[b][a]); 
       glVertex2f(x_brick[b][a], y_brick[b][a] - 0.10); 
       glVertex2f(x_brick[b][a] + 0.2, 
         y_brick[b][a] - 0.10); 
       glVertex2f(x_brick[b][a] + 0.2, y_brick[b][a]); 
      } 
     } 
    } 
    glEnd(); 
} 

void ball_draw() 
{ 
    glColor3f(0.0, 0.0, 0.0); 
    glBegin(GL_TRIANGLE_FAN); 
    glVertex2f(x_pos, y_pos); 
    for (float angle = 0; angle < (10); angle += 0.01) { 
     glVertex2f((x_pos + sin(angle) * RADIUS), 
       (y_pos + (cos(angle)) * RADIUS)); 
    } 
    glEnd(); 
} 

bool brick_hit() 
{ 
    for (int a = 0; a < 10; a++) { 
     for (int b = 0; b < 4; b++) { 
      if (x_pos >= x_brick[b][a] 
       && x_pos <= x_brick[b][a] + 0.2) { 
       if (y_pos <= y_brick[b][a] 
        && y_pos >= y_brick[b][a] - 0.1) { 
        if (show[b][a] == 1) { 
         show[b][a] = 0; 
         return true; 
        } 
       } 
      } 
     } 
    } 
    return false; 
} 

bool crashed() 
{ 
    if (y_pos < paddle_y - 0.05) 
     return true; 

    return false; 
} 

void paddle_hit() 
{ 
    phit_corner = false; 
    phit_center = false; 
    if (x_pos <= paddle_x + 0.13 && x_pos >= paddle_x - 0.13) { 
     if (y_pos <= paddle_y) { 
      phit_center = true; 
     } 
    } else if ((x_pos >= paddle_x + 0.13 && x_pos <= paddle_x + 0.2) || 
      (x_pos <= paddle_x - 0.13 && x_pos >= paddle_x - 0.2)) { 
     if (y_pos <= paddle_y) { 
      phit_corner = true; 
     } 
    } 
} 

void paddle_move(float dT) 
{ 

    if (paddle_x < RATIO && paddle_x > -RATIO) 
     paddle_x += paddle_speed * PaddleSpeedFactor * dT; 

    if (paddle_x > 1.) { 
     paddle_x = 1.; 
     paddle_speed = 0.; 
    } 

    if (paddle_x < -1.) { 
     paddle_x = -1.; 
     paddle_speed = 0.; 
    } 
} 

void ball_move(float dT) 
{ 
    x_pos += speed_x * dT; 
    y_pos += speed_y * dT; 

    if (brick_hit()) { 
     speed_y *= -1; 
    } 

     if (x_pos >= (RATIO - RADIUS) || x_pos <= (-RATIO + RADIUS)) { 
     speed_x *= -1; 
    } 

    if (y_pos >= (1. - RADIUS)) { 
     speed_y = -1; 
    } 

    paddle_hit(); 
    if (phit_center) { 
     speed_y = 1; 
    } 
    if (phit_corner) { 
     speed_x *= -1; 
     speed_y = 1; 
    } 
} 

void paddle_draw() 
{ 
    glColor3f(0.0, 0.0, 0.0); 
    glBegin(GL_QUADS); 
    glVertex2f(paddle_x - 0.2, paddle_y); 
    glVertex2f(paddle_x + 0.2, paddle_y); 
    glVertex2f(paddle_x + 0.2, paddle_y - 0.05); 
    glVertex2f(paddle_x - 0.2, paddle_y - 0.05); 
    glEnd(); 
} 

void reset_game() 
{ 
    lifes = 3; 
    speed_x = 0; 
    speed_y = 0; 
    x_pos = 0; 
    y_pos = -0.7; 
    paddle_speed = 0; 
    paddle_x = 0; 
} 

void step_game(float dT) 
{ 
    if(!lifes) 
     return; 

    paddle_move(dT * SpeedFactor); 
    ball_move(dT * SpeedFactor); 

    if (crashed()) { 
     lifes--; 
     speed_x = 0; 
     speed_y = 0; 
     x_pos = 0; 
     y_pos = -0.7; 
    } 
} 

void launch_ball() 
{ 
    if(!lifes) 
     return; 

    speed_y = 1.; 
    speed_x = 1.; 
} 

void keyboard(int key, int action) 
{ 
    switch(key) 
    { 
    case GLFW_KEY_ENTER: 
     launch_ball(); 
     break; 

    case GLFW_KEY_ESC: 
     reset_game(); 
     break; 

    case GLFW_KEY_LEFT: 
     switch(action) { 
     case GLFW_PRESS: 
      paddle_speed = -1.; 
      break; 

     case GLFW_RELEASE: 
      paddle_speed = 0; 
      break; 
     } break; 

    case GLFW_KEY_RIGHT: 
     switch(action) { 
     case GLFW_PRESS: 
      paddle_speed = 1.; 
      break; 

     case GLFW_RELEASE: 
      paddle_speed = 0; 
      break; 
     } break; 
    } 
} 

void set_xy() 
{ 
    for (int a = 0; a < 5; a++) { 
     for (int b = 0; b < 10; b++) { 
      show[a][b] = 1; 
     } 
    } 
    int c = 0; 
    for (float a = -0.94; c <= 8; a += 0.21) { 

     for (int b = 0; b <= 5; b++) { 
      x_brick[b][c] = a; 

     } 
     c++; 
    } 
    int d = 0; 
    for (float s = 0.99; d <= 3; s -= 0.11) { 
     for (int r = 0; r < 9; r++) { 
      y_brick[d][r] = s; 
     } 
     d++; 
    } 
} 

float display() 
{ 
    int win_width; 
    int win_height; 
    glfwGetWindowSize(&win_width, &win_height); 
    const float win_aspect = (float)win_width/(float)win_height; 

    glfwSetTime(0.); 

    glViewport(0, 0, win_width, win_height); 

    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    if (win_aspect > RATIO) { 
     glOrtho(-win_aspect, win_aspect, -1., 1., -1., 1.); 
    } else { 
     glOrtho(-RATIO, RATIO, -RATIO/win_aspect, RATIO/win_aspect, 
      -1., 1.); 
    } 

    glMatrixMode(GL_MODELVIEW); 

    glClearColor(0., 0., 1., 1.); 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

    glBegin(GL_QUADS); 
    glColor3f(1, 1, 1); 
    glVertex2f(-RATIO, -1); 
    glVertex2f(RATIO, -1); 
    glVertex2f(RATIO, 1); 
    glVertex2f(-RATIO, 1); 
    glEnd(); 

    draw_bricks(); 
    paddle_draw(); 

    ball_draw(); 

    glfwSwapBuffers(); 
    return glfwGetTime(); 
} 

int main(int argc, char **argv) 
{ 
    set_xy(); 

    if(GL_FALSE == glfwInit()) 
     return -1; 

    if(GL_FALSE == glfwOpenWindow(800, 600, 8, 8, 8, 8, 0, 0, GLFW_WINDOW)) 
     return -2; 

    glfwSetWindowTitle("Viraj's Brick Breaker - GLFW version by datenwolf"); 
    glfwSetKeyCallback(keyboard); 

    reset_game(); 

    while(glfwGetWindowParam(GLFW_OPENED)) { 
     glfwPollEvents(); 
     float const dT = display(); 
     step_game(dT); 
    } 

    glfwTerminate(); 

    return 0; 
} 
+0

Eh bien merci pour ça :) Mais ça ne fixe pas la palette étant un peu saccadée et lente. – viraj

+0

@viraj: La palette étant saccadée et lente est un problème avec votre gestion des entrées. Il semble que vous fassiez partiellement la logique du jeu dans la fonction d'affichage. Pas l'endroit idéal pour le faire. Placez la logique du jeu dans la fonction inactive, utilisez les callbacks d'entrée pour définir les drapeaux d'entrée (les touches sont dépressurées, les boutons de souris dépressés, etc., les gestionnaires de déplacement de la souris doivent accumuler les mouvements) sur l'état du jeu. Mesurez également le temps nécessaire pour afficher et afficher un cadre et utilisez-le pour avancer votre temps de jeu. En substance, vous ne devriez pas mélanger le rendu et la logique du jeu - corrigez cela. – datenwolf

+0

Aussi, je veux rendre 3 coeurs en bas. J'ai utilisé des équations pour tracer des points et dessiner une forme de coeur. Je mets cela dans la fonction de lecture, mais cela rend les choses extrêmement lentes. fonctions: mod (x) -sqrt (1-x * x); mod (x) + sqrt (1-x * x); – viraj

0

Je pense que votre distorsion est due à votre fonction Modif.dim (...): on calcule le rapport d'aspect de la fenêtre, mais vous ne l'utilisez pas. Jetez un oeil sur le code où vous avez volé cet extrait de :)

Changez-vous la résolution de votre écran lorsque vous passez en mode plein écran? Si vous le faites, il est très probable qu'un pixel ne soit plus carré, vous aurez donc besoin du format réel de votre écran.