main.cpp
#include <iostream>
#include <stdlib.h>
#include <string>
//include OpenGL header files
#include <GL/glut.h>
//include custom header files
// faster system for bidimensional dynamic arrays
#include "biarray.h"
#include <iostream>
#include <stdlib.h> //Needed for "exit" function
//LEVEL EDIT VARS
//size of tiles
float tilesize = 1.0;
//xyz coordinates top-left of the map will be
float maporiginx = 3.0;
float maporiginy = 3.0;
float maporiginz = 100.0;
//2d array of the map
biarray map(100,100);
//when a key is pressed
void handleKeypress(unsigned char key, int x, int y)
{
switch (key) {
case 27: // ESCAPE
exit(0); //exit
}
}
//rendering
void initRendering() {
//draw objects based on depth
glEnable(GL_DEPTH_TEST);
}
//when the window is resized
void handleResize(int w, int h) {
//convert xyz coordinates to pixel values
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION); //start setting camera perspective
//camera perspective
glLoadIdentity(); //reset camera
gluPerspective(45.0, //The camera angle
(double)w / (double)h, //The width-to-height ratio
1.0, //how close something is before it's not rendered
200.0); //how far something is before it's not rendered
}
/****************************
THE DRAW SCENE FUNCTION
THIS IS THE FUNCTION I
AM HAVING PROBLEMS WITH
****************************/
void drawScene()
{
float xcoord = 0;
float ycoord = 0;
float zcoord = 0;
//Clear information from last draw
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
glLoadIdentity(); //Reset the drawing perspective
int mpwidth = map.Width();
int mpheight = map.Height();
for(int i=0;i<mpwidth;i++)
{
xcoord = maporiginx + (tilesize*i);
ycoord = maporiginy;
zcoord = maporiginz + map.Get(xcoord,ycoord);
std::cout << xcoord << ", " << ycoord << ", " << zcoord << std::endl;
/*********************/
/* top left triangle */
glBegin(GL_TRIANGLES);
glVertex3f((xcoord), (ycoord), (zcoord));
glVertex3f((xcoord+tilesize), (ycoord), (zcoord));
glVertex3f((xcoord), (ycoord+tilesize), (zcoord));
glEnd();
/*************************/
/* bottom right triangle */
glBegin(GL_TRIANGLES);
glVertex3f((xcoord+tilesize), (ycoord), (zcoord));
glVertex3f((xcoord+tilesize), (ycoord+tilesize), (zcoord));
glVertex3f((xcoord), (ycoord+tilesize), (zcoord));
glEnd();
for(int b=0;b<mpheight;b++)
{
xcoord = maporiginx + (tilesize*i);
ycoord = maporiginy + (tilesize*b);
zcoord = maporiginz + map.Get(xcoord,ycoord);
/*********************/
/* top left triangle */
glBegin(GL_TRIANGLES);
glVertex3f((xcoord), (ycoord), (zcoord));
std::cout << xcoord << ", " << ycoord << ", " << zcoord << std::endl;
glVertex3f((xcoord+tilesize), (ycoord), (zcoord));
std::cout << xcoord+tilesize << ", " << ycoord << ", " << zcoord << std::endl;
glVertex3f((xcoord), (ycoord+tilesize), (zcoord));
std::cout << xcoord << ", " << ycoord+tilesize << ", " << zcoord << std::endl;
glEnd();
/*************************/
/* bottom right triangle */
glBegin(GL_TRIANGLES);
glVertex3f((xcoord+tilesize), (ycoord), (zcoord));
glVertex3f((xcoord+tilesize), (ycoord+tilesize), (zcoord));
glVertex3f((xcoord), (ycoord+tilesize), (zcoord));
glEnd();
}
}
glutSwapBuffers(); //send 3d scene to the screen
}
int main(int argc, char** argv)
{
//Initialize GLUT
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(800, 600); //Set the window size
//Create the window
glutCreateWindow("Map Editor");
initRendering(); //Initialize rendering
//Set handler functions for drawing, keypresses, and window resizes
glutDisplayFunc(drawScene);
glutKeyboardFunc(handleKeypress);
glutReshapeFunc(handleResize);
glutMainLoop(); //Start the main loop. glutMainLoop doesn't return.
return 0; //This line is never reached
}
biarray.h
#include <iostream>
/*
* biarray class was written by
* M_D_Kodar after about 5
* failed attempts from me
*/
class biarray
{
public:
biarray(int rowsl, int colsl)
{
//create an array
Create(rowsl, colsl);
};
~biarray()
{
//delete the array
Delete();
};
void Clear()
{
//clear array
for(int a=0;a<rows;a++)
{
for(int b=0;b<cols;b++)
{
array[a] = 0;
}
}
};
void Resize(int rowsl, int colsl)
{
//resize array
Delete();
Create(rowsl, colsl);
};
void Set(int x, int y, int val)
{
//set x,y coordinate in the 2d array to a value
array[x][y] = val;
};
int Get(int x, int y)
{
//get x,y coordinate in the 2d array
return array[x][y];
};
int Width()
{
return cols;
};
int Height()
{
return rows;
}
private:
void Create(int rowsl, int colsl)
{
array = (int**)malloc(sizeof(int*) * rowsl);
for(int i=0;i<rowsl;i++)
{
array = (int*)malloc(sizeof(int) * colsl);
}
rows = rowsl;
cols = colsl;
};
void Delete()
{
for(int i=0;i<rows;i++)
{
free(array);
}
};
int **array;
int rows,cols;
};
I'm not sure if you will need the 'biarray.h' file, but just in case, there ya go.
Alright, so I am trying to make a map editor, and I am having trouble with looping through the map array to load the terrain it gets up to line 96, but then there is a segmentation fault. I'm pretty much a newbie to this stuff, so if someone could explain what that is if I'm wrong: I think it means you try to allocate something to a memory address which does not exist. I don't see any possible way I could be doing this here.
It's output is that the for loop continues loops right after i == 96, but the xcoord, etc. stay the same, which is odd, because if I do tilesize*i right after it w/o setting it to a variable or anything, it works, which makes me think it's something wrong with the "float" data type, so I decided to try "int", and just divide by 10 when I set the vertex positions. This didn't work either.
Could someone please tell me what I'm doing wrong with my code?
And before someone asks:
I did try defining xcoord, ycoord, and zcoord variables before the for loop in the function, so yeah.
Sorry for the long post, and sorry again if I'm doing something really stupid here!
Edit:
Sorry if the #include's got messed up, dunno what happened.
[Edited by - logank9 on July 25, 2009 2:28:53 PM]