PDA

View Full Version : A question about sizing



sisolina_23
06-12-2012, 02:18 PM
I am making a game "Frozen Bubbles", but when I maximize the window, the initial colors of the spheres change. What can be the problem?
This is my source code:
#include "stdafx.h"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <gl/glut.h>
using namespace std;
GLfloat Array[8][16][3];
GLfloat r[3];
int button, state, x, y;
const float w=304.0, h=272.0;
void init()
{
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluOrtho2D(0.0f, w, 0.0f, h);
srand((unsigned) time(NULL));
}
// end init()
static void renderSphere(GLfloat p, GLfloat q, GLfloat r)
{
glPushMatrix();
glTranslatef(p, q, r);
glutWireSphere(8, 50, 50);
glPopMatrix();
}
void NextSphere()
{
int q;
glPushMatrix();
GLfloat Color[6][3]={{1, 0.5, 0}, {1, 0, 0}, {1, 0, 1}, {0, 1, 0}, {0, 0, 1}, {1, 1, 0}};
q=GLint(rand())%6;
glColor3f(Color[q][0], Color[q][1], Color[q][2]);
renderSphere(24, 8, 0);
glPopMatrix();
}
void arrow()
{
if ((((x>=0) && (x<=124)) || ((x>=140) && (x<=264))) && ((y>=16) && (y<=272))) {
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_LINES);
glVertex2f(132,16);
glVertex2f(132+6*(x-132)/sqrt((x-132.0)*(x-132.0)+(y-16.0)*(y-16.0)), 16+6*(y-16)/sqrt((x-132.0)*(x-132.0)+(y-16.0)*(y-16.0)));
glEnd();
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_TRIANGLES);
glVertex2f(132+6*(x-132)/sqrt((x-132.0)*(x-132.0)+(y-16.0)*(y-16.0)), 16+6*(y-16)/sqrt((x-132.0)*(x-132.0)+(y-16.0)*(y-16.0)));
glVertex2f((4*(x-132)-2*sqrt(3.0)/3*(y-16))/sqrt((x-132.0)*(x-132.0)+(y-16.0)*(y-16.0))+132, (4*(y-16)+2*sqrt(3.0)/3*(x-132))/sqrt((x-132.0)*(x-132.0)+(y-16.0)*(y-16.0))+16);
glVertex2f((4*(x-132)+2*sqrt(3.0)/3*(y-16))/sqrt((x-132.0)*(x-132.0)+(y-16.0)*(y-16.0))+132, (4*(y-16)-2*sqrt(3.0)/3*(x-132))/sqrt((x-132.0)*(x-132.0)+(y-16.0)*(y-16.0))+16);
glEnd();
}
glFlush();
}

void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glShadeModel(GL_FLAT);
int i, j, p, q;
GLfloat color_array[6][3]={{1, 0.5, 0}, {1, 0, 0}, {1, 0, 1}, {0, 1, 0}, {0, 0, 1}, {1, 1, 0}};

for (i=0; i<8; i++)
{
for (j=0; j<16; j++)
{
p=GLint(rand())%6;
glColor3f(color_array[p][0], color_array[p][1], color_array[p][2]);
Array[i][j][0]=color_array[p][0];
Array[i][j][1]=color_array[p][1];
Array[i][j][2]=color_array[p][2];
if (i%2==0)
renderSphere(8+16*j, 264-16*i, 0);
else
renderSphere(16+16*j, 264-16*i, 0);
}
}
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_LINES);
glVertex2f(0, 48);
glVertex2f(264, 48);
glVertex2f(264, 0);
glVertex2f(264, 272);
glEnd();
GLfloat Color[6][3]={{1, 0.5, 0}, {1, 0, 0}, {1, 0, 1}, {0, 1, 0}, {0, 0, 1}, {1, 1, 0}};
q=GLint(rand())%6;
r[0]=Color[q][0];
r[1]=Color[q][1];
r[2]=Color[q][2];
glColor3f(Color[q][0], Color[q][1], Color[q][2]);
renderSphere(132, 8, 0);
NextSphere();
arrow();


glutSwapBuffers();
}
void mouse(int button, int state, int x, int y)
{
int l=7;
int m=2;
int p, q, v, s, n;
int points=0;
if (button==GLUT_LEFT_BUTTON) {
if (state==GLUT_DOWN) {
if (((136*x+132*y-20064)/(y-16)>0) && ((136*x+132*y-20064)/(y-16)<264)) {
glColor3f(r[0], r[1], r[2]);

glTranslatef((136*x-17592)/(y-16), 128, 0);
renderSphere(132, 8, 0);
for (int j=0; j<16; j++) {
if (l%2) {
if ((136*x+132*y-20064)/(y-16)>8+j*16 && (136*x+132*y-20064)/(y-16)<24+j*16){
if ((r[0]==Array[j][l][0]) && (r[1]==Array[j][l][1]) && (r[2]==Array[j][l][2]))
{
do
{
v=l--;
if ((Array[j][l][0]==Array[j][v][0]) && (Array[j][l][1]==Array[j][v][1]) && (Array[j][l][2]==Array[j][v][2]))
{
m++;
glColor3f(1.0f, 1.0f, 1.0f);
renderSphere((136*x+132*y-19008)/(y-16), 136, 0);
renderSphere(j*16+16-(136*x+132*y-19008)/(y-16), 16, 0);
renderSphere(0, 128-16*(v+1), 0);
do
{
q=j++;
if ((Array[j][v][0]==Array[q][v][0]) && (Array[j][v][1]==Array[q][v][1]) && (Array[j][v][2]==Array[q][v][2]))
{
m++;
if (v%2)
{
glColor3f(1.0f, 1.0f, 1.0f);
renderSphere(16*(q-j), 0, 0);
}
else
{
glColor3f(1.0f, 1.0f, 1.0f);
renderSphere(16*(q-j)-8, 0, 0);
}
}
}
while ((q<15) || ((Array[j][v][0]!=Array[q][v][0]) || (Array[j][v][1]!=Array[q][v][1]) || (Array[j][v][2]!=Array[q][v][2])));
do
{
p=j--;
if ((Array[j][v][0]==Array[p][v][0]) && (Array[j][v][1]==Array[p][v][1]) && (Array[j][v][2]==Array[p][v][2]))
{
m++;
if (v%2) {
glColor3f(1.0f, 1.0f, 1.0f);
renderSphere(16*(p-j), 0, 0);
}
else
{
glColor3f(1.0f, 1.0f, 1.0f);
renderSphere((p-j)*16-8, 0, 0);
}
}
}
while ((p>0) || ((Array[j][v][0]!=Array[p][v][0]) || (Array[j][v][1]!=Array[p][v][1]) || (Array[j][v][2]!=Array[p][v][2])));
}
}
while ((v>0) || ((Array[j][l][0]!=Array[j][v][0]) || (Array[j][l][1]!=Array[j][v][1]) || (Array[j][l][2]!=Array[j][v][2])));
}
}
}
else
{
if ((136*x+132*y-20064)/(y-16)>j*16 && (136*x+132*y-20064)/(y-16)<16+j*16)
{
if ((r[0]==Array[j][l][0]) && (r[1]==Array[j][l][1]) && (r[2]==Array[j][l][2]))
{
do
{
v=l--;
if ((Array[j][l][0]==Array[j][v][0]) && (Array[j][l][1]==Array[j][v][1]) && (Array[j][l][2]==Array[j][v][2]))
{
m++;
glColor3f(1.0, 1.0, 1.0);
renderSphere((136*x+132*y-19008)/(y-16), 136, 0);
renderSphere(8+j*16-(136*x+132*y-19008)/(y-16), 16, 0);
renderSphere(0, 128-16*(v+1), 0);
do
{
q=j++;
if ((Array[j][v][0]==Array[q][v][0]) && (Array[j][v][1]==Array[q][v][1]) && (Array[j][v][2]==Array[q][v][2]))
{
m++;
if (v%2)
{
glColor3f(1.0f, 1.0f, 1.0f);
renderSphere(16*(q-j), 0, 0);
}
else
{
glColor3f(1.0f, 1.0f, 1.0f);
renderSphere(16*(q-j)-8, 0, 0);
}
}
}
while ((q<15) || ((Array[j][v][0]!=Array[q][v][0]) || (Array[j][v][1]!=Array[q][v][1]) || (Array[j][v][2]!=Array[q][v][2])));
do
{
p=j--;
if ((Array[j][v][0]==Array[p][v][0]) && (Array[j][v][1]==Array[p][v][1]) && (Array[j][v][2]==Array[p][v][2]))
{
m++;
if (v%2)
{
glColor3f(1.0f, 1.0f, 1.0f);
renderSphere(16*(p-j), 0, 0);
}
else
{
glColor3f(1.0f, 1.0f, 1.0f);
renderSphere(16*(p-j)-8, 0, 0);
}
}
}
while ((p>0) || ((Array[j][v][0]!=Array[p][v][0]) || (Array[j][v][1]!=Array[p][v][1]) || (Array[j][v][2]!=Array[p][v][2])));
}
}
while ((v>0) || ((Array[j][l][0]!=Array[j][v][0]) || (Array[j][l][1]!=Array[j][v][1]) || (Array[j][l][2]!=Array[j][v][2])));
}
}
}
do
{
s=j++;
if ((Array[j][l][0]==Array[s][l][0]) && (Array[j][l][1]==Array[s][l][1]) && (Array[j][l][2]==Array[s][l][2]))
{
m++;
glColor3f(1.0f, 1.0f, 1.0f);
renderSphere(s*16+8, 152, 0);
do
{
v=l--;
if ((Array[j][l][0]==Array[j][v][0]) && (Array[j][l][1]==Array[j][v][1]) && (Array[j][l][2]==Array[j][v][2]))
{
m++;
glColor3f(1.0f, 1.0f, 1.0f);
renderSphere(8, 16, 0);
}
}
while ((v>0) || ((Array[j][l][0]!=Array[j][v][0]) || (Array[j][l][1]!=Array[j][v][1]) || (Array[j][l][2]!=Array[j][v][2])));
}
}
while ((s<15) || ((Array[j][l][0]!=Array[s][l][0]) || (Array[j][l][1]!=Array[s][l][1]) || (Array[j][l][2]!=Array[s][l][2])));
do
{
n=j--;
if ((Array[j][l][0]==Array[n][l][0]) && (Array[j][l][1]==Array[n][l][1]) && (Array[j][l][2]==Array[n][l][2]))
{
m++;
glColor3f(1.0f, 1.0f, 1.0f);
renderSphere(n*16+8, 152, 0);
do
{
int v=l--;
if ((Array[j][l][0]==Array[j][v][0]) && (Array[j][l][1]==Array[j][v][1]) && (Array[j][l][2]==Array[j][v][2]))
{
m++;
glColor3f(1.0f, 1.0f, 1.0f);
renderSphere(8, 16, 0);
}
}
while ((v>0) || ((Array[j][l][0]!=Array[j][v][0]) || (Array[j][l][1]!=Array[j][v][1]) || (Array[j][l][2]!=Array[j][v][2])));
}
}
while ((n>0) || ((Array[j][l][0]!=Array[n][l][0]) || (Array[j][l][1]!=Array[n][l][1]) || (Array[j][l][2]!=Array[j][v][2])));
}
}
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(w, h);
glutCreateWindow("Frozen Bubbles");
init();
glutReshapeFunc(NULL);
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMainLoop();
return 0;
}

Dark Photon
06-12-2012, 06:40 PM
First, surround your code with [ code] [ /code] blocks (without the spaces) to make it readable.
Also, there's a lot of code here. You can work to thin down the code so that it is shorter but yet reproduces the problem. In doing so, you'll find out what was and wasn't related to the problem, and (if we're alike) usually find and fix the problem yourself in the process.

sisolina_23
06-12-2012, 10:50 PM
How can I forbid the maximization of the window?

Dark Photon
06-14-2012, 05:20 AM
How can I forbid the maximization of the window?
You're using GLUT for window control, so I'm not sure you have that option. If you do (since you're using Windows) it's probably going to be some windows specific hack (check with the folks in the Windows forum).

It's better to just handle it when it happens (i.e. install a reshape callback which changes the viewport and projection matrix and posts a redisplay event).

mhagain
06-14-2012, 07:43 AM
How can I forbid the maximization of the window?

That's not the solution. You need to find out why your colours change and fix that. I'm not inclined to go through your code and reformat it so that it's readable, so if you could re-post with proper code tags it might help us to help you.

Dan Bartlett
06-14-2012, 08:19 AM
A few problems:

(1)Your init function is wrong:


void init()
{
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluOrtho2D(0.0f, w, 0.0f, h);
srand((unsigned) time(NULL));
}

it should probably be:

void init()
{
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0f, w, 0.0f, h);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
srand((unsigned) time(NULL));
}


(2) You have drawing code inside your "mouse" function, whereas it's better to just record the events in the mouse/keyboard functions, and only draw them in the "display" function.

(3) You have a "glTranslatef" call in your mouse function, that isn't protected with a glPushMatrix/glPopMatrix + you don't reset the modelview matrix anywhere, so every frame rendered will translate the scene.

Beyond that, it's hard to read without the [ code ] [ / code ] tags + a little too much irrelevant information to delve too deeply into.

sisolina_23
06-17-2012, 11:35 PM
Thank you! The problem isn't the resizement. I generated the random colors of the spheres in other function and the problem was decided.