OpenGL 2d tile engine ...
Well this is tha code for 2d-programming "using tiles" in OpenGL.
Now I wonder if anyone could help me figure how to make it use more tiles (doing as I did with the first two gives me helluwa weird errors)...
// DEFINES ////////////////////////////////////////////////////////////////////
#define WIN32_LEAN_AND_MEAN
#define MAPSIZEX 10
#define MAPSIZEY 10
// INCLUDES ///////////////////////////////////////////////////////////////////
#include
#include
#include
#include
#include
#include
// GLOBALS ////////////////////////////////////////////////////////////////////
HDC g_HDC; //Header of global device context
float angle = 0.0f; //Angle variable used fro rotation
// GLOBALS FOR TEXTURES ///////////////////////////////////////////////////////
GLuint texture[2];
// MAP THAT WE RENDER) ////////////////////////////////////////////////////////
char map[10][10] = {
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
};
// FUNCTION USED TO LOAD BITMAP FILES /////////////////////////////////////////
AUX_RGBImageRec *LoadBmp(char *Filename) // Loads A Bitmap Image
{
FILE *File=NULL; // File Handle
if (!Filename) // Make Sure A Filename Was Given
{
return NULL; // If Not Return NULL
}
File=fopen(Filename,"r"); // Check To See If The File Exists
if (File) // Does The File Exist?
{
fclose(File); // Close The Handle
return auxDIBImageLoad(Filename); // Load The Bitmap And Return A Pointer
}
return NULL; // If Load Failed Return NULL
}
// LOAD TEXTURES INTO MEMORY //////////////////////////////////////////////////
int LoadGlTextures()
{
AUX_RGBImageRec *TextureImage[2];
memset(TextureImage,0,sizeof(void *)*1);
TextureImage[0] = LoadBmp("tile.bmp");
TextureImage[1] = LoadBmp("tile1.bmp");
glGenTextures(2, &texture[0]); //We are generating 2 textures...
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX,
TextureImage[0]->sizeY, 0, GL_RGB,
GL_UNSIGNED_BYTE, TextureImage[0]->data);
glBindTexture(GL_TEXTURE_2D, texture[1]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[1]->sizeX,
TextureImage[1]->sizeY, 0, GL_RGB,
GL_UNSIGNED_BYTE, TextureImage[1]->data);
for (int t = 0; t < 2; t++)
{
if (TextureImage[t])
{
if (TextureImage[t]->data)
{
free(TextureImage[t]->data);
}
free(TextureImage[t]);
}
}
return(1);
}
// FUNCTION TO DRAW ALL THE TILES /////////////////////////////////////////////
int DrawTiles(GLvoid)
{
int tile;
for (int y = 0; y < MAPSIZEY; y++)
{
for (int x = 0; x < MAPSIZEX; x++)
{
tile = map[y][x];
glBindTexture(GL_TEXTURE_2D, texture[tile]);
glBegin(GL_QUADS);
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(float(x), float(y), 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(float(x + 1), float(y), 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(float(x + 1), float(y + 1), 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(float(x), float(y + 1), 0.0f);
glEnd();
}
}
return(1);
}
// FUNCTION TO SET UP THE PIXELFORMAT FOR THE DEVICE CONTEXT //////////////////
void SetupPixelFormat(HDC hDC)
{
int nPixelFormat; //The Pixel format index
static PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL |
PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
32,
0, 0, 0, 0, 0, 0,
0,
0,
0,
0, 0, 0, 0,
16,
0,
0,
PFD_MAIN_PLANE,
0,
0, 0, 0 };
//Choose best matching pixel format, return index
nPixelFormat = ChoosePixelFormat(hDC, &pfd);
//Set pixel format to device context, yo!
SetPixelFormat(hDC, nPixelFormat, &pfd);
}
// THE WINDOWS PROCEDURE MESSAGE HANDLER //////////////////////////////////////
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
//Variables
static HGLRC hRC;
static HDC hDC;
int width, height;
//Message handler
switch(message)
{
case WM_CREATE:
hDC = GetDC(hwnd);
g_HDC = hDC;
SetupPixelFormat(hDC);
//Create rendering context and make it current:
hRC = wglCreateContext(hDC);
wglMakeCurrent(hDC, hRC);
//Enable blending:
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
if (!LoadGlTextures()) // Jump To Texture Loading Routine ( NEW )
{
return 0; // If Texture Didn''t Load Return FALSE
}
glEnable(GL_TEXTURE_2D);
return 0;
break;
case WM_CLOSE:
//Deselect rendering context and delete it:
wglMakeCurrent(hDC, NULL);
wglDeleteContext(hRC);
PostQuitMessage(0);
return 0;
break;
case WM_SIZE:
height = HIWORD(lParam); //retrive height and width
width = LOWORD(lParam);
if (height==0) //make divide by zero imposible
{
height=1;
}
//reset tha viewport to new dimensions:
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//calculate aspect ratio of window
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,1.0f,1000.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
return 0;
break;
default:
break;
}
return (DefWindowProc(hwnd, message, wParam, lParam));
}
// THE PROGRAM ENTRY POINT, WINMAIN() /////////////////////////////////////////
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nShowCmd)
{
//Varaibles
WNDCLASSEX windowClass; // window class
HWND hwnd; // window handle
MSG msg; // message
bool done; // flag saying when the app is finished
//Fill out the windows structure:
windowClass.cbSize = sizeof(WNDCLASSEX);
windowClass.style = CS_HREDRAW | CS_VREDRAW;
windowClass.lpfnWndProc = WndProc;
windowClass.cbClsExtra = 0;
windowClass.cbWndExtra = 0;
windowClass.hInstance = hInstance;
windowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
windowClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
windowClass.lpszMenuName = NULL;
windowClass.lpszClassName = "MyClass";
windowClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
//Register the windows class:
if (!RegisterClassEx(&windowClass))
return 0;
//Class registred, so now lets create tha window!
hwnd = CreateWindowEx(NULL,
"MyClass",
"OpenGL Basecode",
WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_SYSMENU,
100, 100,
800, 600,
NULL,
NULL,
hInstance,
NULL);
//Check if window creation failed (in other words, does hwnd equal NULL?)
if (!hwnd)
return 0;
ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd);
done = false; //Init tha loop condition varaible!
//Main message loop! (loops over and over until your program quits)
while (!done)
{
PeekMessage(&msg, hwnd, NULL, NULL, PM_REMOVE);
if (msg.message == WM_QUIT) //Is there a quit message in tha que?
{
done = true; //If so, quit!
}
else
{
// Do rendering here...
// Clear screen and depth buffer, and reset camera pos
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(10.0f, 8.0f, 20.0f, 10.0f, 8.0f, 0.0f, 0.0f, 1.0f, 0.0f);
DrawTiles();
SwapBuffers(g_HDC);
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
I think the problem is in your LoadGLTextures function. Instead of memset(TextureImage,0,sizeof(void *)*1); you should have memset(TextureImage,0,sizeof(void *)*2); cause you want allocate memory for two textures.
K.
K.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement