• Advertisement

Archived

This topic is now archived and is closed to further replies.

What's wrong with this code?

This topic is 5718 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, Whenever I try to compile this code, it compiles fine, links fine, so I execute it. As the program begins (in a window, not fullscreen) an error pops up saying I did an illegal command or something. In fullscreen, it just doesn't load. I don't see why it compiles but not runs, and I can't figure out the debugger (just got msvc++). Anyhow, heres the code...
      int load_gl_textures()
{
  AUX_RGBImageRec *TextureImage[3];

  memset(TextureImage,0,sizeof(void *)*1);

  
  TextureImage[3] = loadbmp("blue.bmp");
  TextureImage[1] = loadbmp("cement.bmp");
  TextureImage[2] = loadbmp("52.bmp");
  

  glGenTextures(3, &texture[3]); 

  glBindTexture(GL_TEXTURE_2D, texture[3]);
  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[3]->sizeX,
               TextureImage[3]->sizeY, 0, GL_RGB,
               GL_UNSIGNED_BYTE, TextureImage[3]->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);

  glBindTexture(GL_TEXTURE_2D, texture[2]);
  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[2]->sizeX,
               TextureImage[2]->sizeY, 0, GL_RGB,
               GL_UNSIGNED_BYTE, TextureImage[2]->data);

	for (int t = 0; t < 2; t++)
	{
		if (TextureImage[t])
		{
		if (TextureImage[t]->data)
		{
			free(TextureImage[t]->data);
		}

		free(TextureImage[t]);
		}
	}

    
Any help would be appreciated. Thanks EDIT - got the damn source tags messed up [edited by - PepsiPlease on May 29, 2002 9:19:14 PM] [edited by - PepsiPlease on May 29, 2002 9:20:27 PM]

Share this post


Link to post
Share on other sites
Advertisement
You are overflowing, arrays start on 0 so drop the value by one on each of those assignments.

Share this post


Link to post
Share on other sites
Oops, I posted the wrong code. The first time I tried it I had 3 textures, 0 1 and 2, and the same thing happened, so I tried to change it, to no avail. This is the same exact thing, just after my failed attempt to fix the problem, so there must be another reason it''s happening.

Share this post


Link to post
Share on other sites
If it helps, here is what the debugger said:

//Lots of DLL''s
//More DLL''s

First-chance exception in lesson1.exe: 0xC0000005: Access Violation.





This is probably just a simple mistake, but I can''t seem to figure it out.

Share this post


Link to post
Share on other sites
What line does the code break on? Or, if it breaks into some assembly code hit Alt-7 and bring up the call stack and double click on the function that is not part of Win32, and see where it is being called from there.

Usually when I get this error it is from trying to read something that is not there. Like a pointer that points to nothing, or an array that has no information inside of it.



HighEnd
----
"Becoming a game developer from scratch..."
http://www.highendgaming.com

Share this post


Link to post
Share on other sites
Your right. Except this time it was trying to read a bitmap file from a .psp file.

But when I tryed to compile it, it still didn't work. This time it said "Release Device Context Failed". Here is the revised code.


    #include <windows.h>		// Header File For #include <gl\gl.h>			// Header File For The #include <gl\glu.h>			// Header File For The #include <gl\glaux.h>
#include <stdio.h> // Header File For The Glaux


HDC hDC=NULL; // Private GDI

HGLRC hRC=NULL; // Permanent Rendering

HWND hWnd=NULL; // Holds Our Window e

HINSTANCE hInstance; // Holds The Instance Of


bool keys[256]; // Array Used For The

bool active=TRUE; // Window Active Flag Set To

bool fullscreen=TRUE;


#define MAP_SIZEX 10
#define MAP_SIZEY 10


GLuint texture[3];


char map[10][10] = {
{1, 1, 1, 2, 2, 2, 2, 1, 1, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 2},
{1, 1, 1, 1, 1, 1, 1, 0, 0, 2},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 2},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 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, 1, 1, 1, 1, 1, 1, 1, 1, 1},
};

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // Declaration For WndProc


GLvoid ReSizeGLScene(GLsizei width, GLsizei height) // Resize And Initialize The GL Window

{
if (height==0) // Prevent A Divide By Zero By

{
height=1; // Making Height Equal One

}

glViewport(0,0,width,height); // Reset The Current Viewport


glMatrixMode(GL_PROJECTION); // Select The Projection Matrix

glLoadIdentity(); // Reset The Projection Matrix


// Calculate The Aspect Ratio Of The Window

gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);

glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix

glLoadIdentity(); // Reset The Modelview Matrix

}

AUX_RGBImageRec *loadbmp(char *filename)
{
FILE *file = NULL;

if (!filename)
return NULL;

file = fopen(filename, "r");

if (file)
{
fclose(file);
return auxDIBImageLoad(filename);
}

return NULL;
}



int load_gl_textures()
{
AUX_RGBImageRec *TextureImage[3];

memset(TextureImage,0,sizeof(void *)*1);


TextureImage[0] = loadbmp("blue.bmp");
TextureImage[1] = loadbmp("cement.bmp");
TextureImage[2] = loadbmp("52.bmp");


glGenTextures(3, &texture[3]);

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);

glBindTexture(GL_TEXTURE_2D, texture[2]);
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[2]->sizeX,
TextureImage[2]->sizeY, 0, GL_RGB,
GL_UNSIGNED_BYTE, TextureImage[2]->data);

for (int t = 0; t < 3; t++)
{
if (TextureImage[t])
{
if (TextureImage[t]->data)
{
free(TextureImage[t]->data);
}

free(TextureImage[t]);
}
}

return(1);
}


int draw_tiles(GLvoid)
{
int tile;
for (int y = 0; y < MAP_SIZEY; y++)
{
for (int x = 0; x < MAP_SIZEX; x++)
{
tile = map[y][x];



glBindTexture(GL_TEXTURE_2D, texture[tile]);
glBegin(GL_QUADS);
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);
}

int InitGL(GLvoid) // All Setup For OpenGL Goes Here

{
if(!load_gl_textures())
{
return FALSE;
}
glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH); // Enable Smooth Shading

glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background

glClearDepth(1.0f); // Depth Buffer Setup

glEnable(GL_DEPTH_TEST); // Enables Depth Testing

glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do

glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations

return TRUE; // Initialization Went OK

}

int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing

{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(5.0f, 8.0f, 20.0f, 5.0f, 8.0f, 0.0f, 0.0f, 1.0f, 0.0f);
draw_tiles();
return TRUE;
}


//And the rest of the code...






Why does it say that message?


Thanks

[edited by - PepsiPlease on May 29, 2002 10:07:18 PM]

Share this post


Link to post
Share on other sites
...and here is the new debug output:

The thread 0xFAE123ED has exited with code 0 (0x0).
The program ''C:\OpenGL\Lesson1\Debug\lesson1.exe'' has exited with code 0 (0x0).

Share this post


Link to post
Share on other sites
You're still accessing memory that is out-of-bounds


glGenTextures(3, &texture[3]);


Also, if your trying to pass in an array (as the function calls for). Try the following:


glGenTextures(3, texture);


[edited by - aNonamuss on May 29, 2002 10:40:54 PM]

Share this post


Link to post
Share on other sites
It works!

Thanks a lot for to help. I am just learning opengl, so a lot of this stuff is confusing.

monkey - I am just messing around with opengl, trying to learn the basics and such. If you have seen my other posts, I was trying to make a map loading function (which I never got working).



Again, thanks for the help

Share this post


Link to post
Share on other sites
yes, your tile engine draws tiles mirrored horizontally

I''ve been trying to trace it to figure out what''s wrong with it, but I can''t find why its doing that! Any help from anybody? Here''s my modified source:

  
#include <windows.h>
#include <gl\gl.h>
#include <gl\glu.h>
#include <gl\glaux.h>
#include <stdio.h>

HDC hDC=NULL;
HGLRC hRC=NULL;
HWND hWnd=NULL;
HINSTANCE hInstance;
bool keys[256];
bool active=TRUE;
bool fullscreen=TRUE;


#define MAP_SIZEX 16
#define MAP_SIZEY 12

GLuint texture[2];

char map[12][16]={ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1, };



LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
GLvoid ReSizeGLScene(GLsizei width, GLsizei height)
{
if (height==0)
{
height=1;
}

glViewport(0,0,width,height);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();


gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

AUX_RGBImageRec *loadbmp(char *filename)
{
FILE *file = NULL;

if (!filename)
return NULL;

file = fopen(filename, "r");

if (file)
{
fclose(file);
return auxDIBImageLoad(filename);
}

return NULL;
}



int load_gl_textures()
{
AUX_RGBImageRec *TextureImage[2];

memset(TextureImage,0,sizeof(void *)*1);

TextureImage[0] = loadbmp("data\\tile.bmp");
TextureImage[1] = loadbmp("data\\tile2.bmp");

glGenTextures(2, &texture[0]); // We are generating two 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);
}


int draw_tiles(GLvoid)
{
int tile;
for (int y=0; y<MAP_SIZEY;y++)
{
for (int x=0; x< MAP_SIZEX; x++)
{
tile = map[y][x];
glBindTexture(GL_TEXTURE_2D, texture[tile]);

glBegin(GL_QUADS);
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);
}

int InitGL(GLvoid) // All Setup For OpenGL Goes Here

{

if(!load_gl_textures())
{
return FALSE;
}
glShadeModel(GL_SMOOTH); // Enable Smooth Shading

glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background

glClearDepth(1.0f); // Depth Buffer Setup

glEnable(GL_DEPTH_TEST); // Enables Depth Testing

glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do

glEnable(GL_TEXTURE_2D);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations

return TRUE; // Initialization Went OK

}

int DrawGLScene(GLvoid) // Here''s Where We Do All The Drawing

{
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);

glTranslatef(-8.0f,-6.0f,-14.5f); // Move Left 1.5 Units And Into The Screen 29.0

draw_tiles();
return TRUE;
}

GLvoid KillGLWindow(GLvoid) // Properly Kill The Window

{
if (fullscreen) // Are We In Fullscreen Mode?

{
ChangeDisplaySettings(NULL,0); // If So Switch Back To The Desktop

ShowCursor(TRUE); // Show Mouse Pointer

}

if (hRC) // Do We Have A Rendering Context?

{
if (!wglMakeCurrent(NULL,NULL)) // Are We Able To Release The DC And RC Contexts?

{
MessageBox(NULL,"Release Of DC And RC Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
}

if (!wglDeleteContext(hRC)) // Are We Able To Delete The RC?

{
MessageBox(NULL,"Release Rendering Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
}
hRC=NULL; // Set RC To NULL

}

if (hDC && !ReleaseDC(hWnd,hDC)) // Are We Able To Release The DC

{
MessageBox(NULL,"Release Device Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
hDC=NULL; // Set DC To NULL

}

if (hWnd && !DestroyWindow(hWnd)) // Are We Able To Destroy The Window?

{
MessageBox(NULL,"Could Not Release hWnd.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
hWnd=NULL; // Set hWnd To NULL

}

if (!UnregisterClass("OpenGL",hInstance)) // Are We Able To Unregister Class

{
MessageBox(NULL,"Could Not Unregister Class.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
hInstance=NULL; // Set hInstance To NULL

}
}

/* This Code Creates Our OpenGL Window. Parameters Are: *
* title - Title To Appear At The Top Of The Window *
* width - Width Of The GL Window Or Fullscreen Mode *
* height - Height Of The GL Window Or Fullscreen Mode *
* bits - Number Of Bits To Use For Color (8/16/24/32) *
* fullscreenflag - Use Fullscreen Mode (TRUE) Or Windowed Mode (FALSE) */


BOOL CreateGLWindow(char* title, int width, int height, int bits, bool fullscreenflag)
{
GLuint PixelFormat; // Holds The Results After Searching For A Match

WNDCLASS wc; // Windows Class Structure

DWORD dwExStyle; // Window Extended Style

DWORD dwStyle; // Window Style

RECT WindowRect; // Grabs Rectangle Upper Left / Lower Right Values

WindowRect.left=(long)0; // Set Left Value To 0

WindowRect.right=(long)width; // Set Right Value To Requested Width

WindowRect.top=(long)0; // Set Top Value To 0

WindowRect.bottom=(long)height; // Set Bottom Value To Requested Height


fullscreen=fullscreenflag; // Set The Global Fullscreen Flag


hInstance = GetModuleHandle(NULL); // Grab An Instance For Our Window

wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; // Redraw On Size, And Own DC For Window.

wc.lpfnWndProc = (WNDPROC) WndProc; // WndProc Handles Messages

wc.cbClsExtra = 0; // No Extra Window Data

wc.cbWndExtra = 0; // No Extra Window Data

wc.hInstance = hInstance; // Set The Instance

wc.hIcon = LoadIcon(NULL, IDI_WINLOGO); // Load The Default Icon

wc.hCursor = LoadCursor(NULL, IDC_ARROW); // Load The Arrow Pointer

wc.hbrBackground = NULL; // No Background Required For GL

wc.lpszMenuName = NULL; // We Don''t Want A Menu

wc.lpszClassName = "OpenGL"; // Set The Class Name


if (!RegisterClass(&wc)) // Attempt To Register The Window Class

{
MessageBox(NULL,"Failed To Register The Window Class.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE

}

if (fullscreen) // Attempt Fullscreen Mode?

{
DEVMODE dmScreenSettings; // Device Mode

memset(&dmScreenSettings,0,sizeof(dmScreenSettings)); // Makes Sure Memory''s Cleared

dmScreenSettings.dmSize=sizeof(dmScreenSettings); // Size Of The Devmode Structure

dmScreenSettings.dmPelsWidth = width; // Selected Screen Width

dmScreenSettings.dmPelsHeight = height; // Selected Screen Height

dmScreenSettings.dmBitsPerPel = bits; // Selected Bits Per Pixel

dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;

// Try To Set Selected Mode And Get Results. NOTE: CDS_FULLSCREEN Gets Rid Of Start Bar.

if (ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL)
{
// If The Mode Fails, Offer Two Options. Quit Or Use Windowed Mode.

if (MessageBox(NULL,"The Requested Fullscreen Mode Is Not Supported By\nYour Video Card. Use Windowed Mode Instead?","NeHe GL",MB_YESNO|MB_ICONEXCLAMATION)==IDYES)
{
fullscreen=FALSE; // Windowed Mode Selected. Fullscreen = FALSE

}
else
{
// Pop Up A Message Box Letting User Know The Program Is Closing.

MessageBox(NULL,"Program Will Now Close.","ERROR",MB_OK|MB_ICONSTOP);
return FALSE; // Return FALSE

}
}
}

if (fullscreen) // Are We Still In Fullscreen Mode?

{
dwExStyle=WS_EX_APPWINDOW; // Window Extended Style

dwStyle=WS_POPUP; // Windows Style

ShowCursor(FALSE); // Hide Mouse Pointer

}
else
{
dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; // Window Extended Style

dwStyle=WS_OVERLAPPEDWINDOW; // Windows Style

}

AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle); // Adjust Window To True Requested Size


// Create The Window

if (!(hWnd=CreateWindowEx( dwExStyle, // Extended Style For The Window

"OpenGL", // Class Name

title, // Window Title

dwStyle | // Defined Window Style

WS_CLIPSIBLINGS | // Required Window Style

WS_CLIPCHILDREN, // Required Window Style

0, 0, // Window Position

WindowRect.right-WindowRect.left, // Calculate Window Width

WindowRect.bottom-WindowRect.top, // Calculate Window Height

NULL, // No Parent Window

NULL, // No Menu

hInstance, // Instance

NULL))) // Dont Pass Anything To WM_CREATE

{
KillGLWindow(); // Reset The Display

MessageBox(NULL,"Window Creation Error.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE

}

static PIXELFORMATDESCRIPTOR pfd= // pfd Tells Windows How We Want Things To Be

{
sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor

1, // Version Number

PFD_DRAW_TO_WINDOW | // Format Must Support Window

PFD_SUPPORT_OPENGL | // Format Must Support OpenGL

PFD_DOUBLEBUFFER, // Must Support Double Buffering

PFD_TYPE_RGBA, // Request An RGBA Format

bits, // Select Our Color Depth

0, 0, 0, 0, 0, 0, // Color Bits Ignored

0, // No Alpha Buffer

0, // Shift Bit Ignored

0, // No Accumulation Buffer

0, 0, 0, 0, // Accumulation Bits Ignored

16, // 16Bit Z-Buffer (Depth Buffer)

0, // No Stencil Buffer

0, // No Auxiliary Buffer

PFD_MAIN_PLANE, // Main Drawing Layer

0, // Reserved

0, 0, 0 // Layer Masks Ignored

};

if (!(hDC=GetDC(hWnd))) // Did We Get A Device Context?

{
KillGLWindow(); // Reset The Display

MessageBox(NULL,"Can''t Create A GL Device Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE

}

if (!(PixelFormat=ChoosePixelFormat(hDC,&pfd))) // Did Windows Find A Matching Pixel Format?

{
KillGLWindow(); // Reset The Display

MessageBox(NULL,"Can''t Find A Suitable PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE

}

if(!SetPixelFormat(hDC,PixelFormat,&pfd)) // Are We Able To Set The Pixel Format?

{
KillGLWindow(); // Reset The Display

MessageBox(NULL,"Can''t Set The PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE

}

if (!(hRC=wglCreateContext(hDC))) // Are We Able To Get A Rendering Context?

{
KillGLWindow(); // Reset The Display

MessageBox(NULL,"Can''t Create A GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE

}

if(!wglMakeCurrent(hDC,hRC)) // Try To Activate The Rendering Context

{
KillGLWindow(); // Reset The Display

MessageBox(NULL,"Can''t Activate The GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE

}

ShowWindow(hWnd,SW_SHOW); // Show The Window

SetForegroundWindow(hWnd); // Slightly Higher Priority

SetFocus(hWnd); // Sets Keyboard Focus To The Window

ReSizeGLScene(width, height); // Set Up Our Perspective GL Screen


if (!InitGL()) // Initialize Our Newly Created GL Window

{
KillGLWindow(); // Reset The Display

MessageBox(NULL,"Initialization Failed.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE

}

return TRUE; // Success

}

LRESULT CALLBACK WndProc( HWND hWnd, // Handle For This Window

UINT uMsg, // Message For This Window

WPARAM wParam, // Additional Message Information

LPARAM lParam) // Additional Message Information

{
switch (uMsg) // Check For Windows Messages

{
case WM_ACTIVATE: // Watch For Window Activate Message

{
if (!HIWORD(wParam)) // Check Minimization State

{
active=TRUE; // Program Is Active

}
else
{
active=FALSE; // Program Is No Longer Active

}

return 0; // Return To The Message Loop

}

case WM_SYSCOMMAND: // Intercept System Commands

{
switch (wParam) // Check System Calls

{
case SC_SCREENSAVE: // Screensaver Trying To Start?

case SC_MONITORPOWER: // Monitor Trying To Enter Powersave?

return 0; // Prevent From Happening

}
break; // Exit

}

case WM_CLOSE: // Did We Receive A Close Message?

{
PostQuitMessage(0); // Send A Quit Message

return 0; // Jump Back

}

case WM_KEYDOWN: // Is A Key Being Held Down?

{
keys[wParam] = TRUE; // If So, Mark It As TRUE

return 0; // Jump Back

}

case WM_KEYUP: // Has A Key Been Released?

{
keys[wParam] = FALSE; // If So, Mark It As FALSE

return 0; // Jump Back

}

case WM_SIZE: // Resize The OpenGL Window

{
ReSizeGLScene(LOWORD(lParam),HIWORD(lParam)); // LoWord=Width, HiWord=Height

return 0; // Jump Back

}
}

// Pass All Unhandled Messages To DefWindowProc

return DefWindowProc(hWnd,uMsg,wParam,lParam);
}

int WINAPI WinMain( HINSTANCE hInstance, // Instance

HINSTANCE hPrevInstance, // Previous Instance

LPSTR lpCmdLine, // Command Line Parameters

int nCmdShow) // Window Show State

{
MSG msg; // Windows Message Structure

BOOL done=FALSE; // Bool Variable To Exit Loop


// Ask The User Which Screen Mode They Prefer

if (MessageBox(NULL,"Would You Like To Run In Fullscreen Mode?", "Start FullScreen?",MB_YESNO|MB_ICONQUESTION)==IDNO)
{
fullscreen=FALSE; // Windowed Mode

}

// Create Our OpenGL Window

if (!CreateGLWindow("tarun''s tile engine",640,480,16,fullscreen))
{
return 0; // Quit If Window Was Not Created

}

while(!done) // Loop That Runs While done=FALSE

{
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) // Is There A Message Waiting?

{
if (msg.message==WM_QUIT) // Have We Received A Quit Message?

{
done=TRUE; // If So done=TRUE

}
else // If Not, Deal With Window Messages

{
TranslateMessage(&msg); // Translate The Message

DispatchMessage(&msg); // Dispatch The Message

}
}
else // If There Are No Messages

{
// Draw The Scene. Watch For ESC Key And Quit Messages From DrawGLScene()

if (active) // Program Active?

{
if (keys[VK_ESCAPE]) // Was ESC Pressed?

{
done=TRUE; // ESC Signalled A Quit

}
else // Not Time To Quit, Update Screen

{
DrawGLScene(); // Draw The Scene

SwapBuffers(hDC); // Swap Buffers (Double Buffering)

}
}

if (keys[VK_F1]) // Is F1 Being Pressed?

{
keys[VK_F1]=FALSE; // If So Make Key FALSE

KillGLWindow(); // Kill Our Current Window

fullscreen=!fullscreen; // Toggle Fullscreen / Windowed Mode

// Recreate Our OpenGL Window

if (!CreateGLWindow("tarun''s tile engine",640,480,16,fullscreen))
{
return 0; // Quit If Window Was Not Created

}
}
}
}

// Shutdown

KillGLWindow(); // Kill The Window

return (msg.wParam); // Exit The Program

}

Share this post


Link to post
Share on other sites
Besides the allocation of memory of TextureImage , arrays in C starts in position 0, I saw that you use the range 1 to 3 for 3 textures (in TextureImage), you must to use 0 to 2.

Share this post


Link to post
Share on other sites
quote:
Original post by PepsiPlease
Thanks a lot for to help. I am just learning opengl, so a lot of this stuff is confusing.



It''s not really an OpenGL problem, more a C/C++ one. I don''t mean to offend you, but I think you should learn a bit more about C/C++ before learning OpenGL. It may seem dull at first, but then it will be much easier (and faster) for you to learn OGL since you won''t have to care about C/C++ problems.

Share this post


Link to post
Share on other sites
nobody answered my question yet

I don''t think it was clear enough.
His engine draws the tiles mirrored horizontally in the sense that if i supply the matrix with:
1000
0010
it will draw
0010
1000
on screen. I''ve been trying to figure out why it is doing that for 2 days now, and its annoying! Anybody care to help out?

Share this post


Link to post
Share on other sites
Yeah, I began learning C++ awhile back, but then I layed off it for awhile because I couln''t purchase the msvc++ compiler, and I was having issues with dev-c++. Then, I got back into it, bought Microsoft''s compiler and started on opengl. I''ve forgotten a lot of stuff, especially memory allocation and stuff like that, so I should reread my sams book.

Anyways, I noticed that horizontal mirroring thing also, but I just figured I was entering the wrong numbers into my gluLookAt function. I guess that''s not the case, considering you did it with glTranslatef, tarun.

Share this post


Link to post
Share on other sites
got it!

  
#include <windows.h>
#include <gl\gl.h>
#include <gl\glu.h>
#include <gl\glaux.h>
#include <stdio.h>

HDC hDC=NULL;
HGLRC hRC=NULL;
HWND hWnd=NULL;
HINSTANCE hInstance;
bool keys[256];
bool active=TRUE;
bool fullscreen=TRUE;
GLfloat tarun;

#define MAP_SIZEX 16
#define MAP_SIZEY 12

GLuint texture[2];

char map[12][16]={ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1, };



LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
GLvoid ReSizeGLScene(GLsizei width, GLsizei height)
{
if (height==0)
{
height=1;
}

glViewport(0,0,width,height);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();


gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

AUX_RGBImageRec *loadbmp(char *filename)
{
FILE *file = NULL;

if (!filename)
return NULL;

file = fopen(filename, "r");

if (file)
{
fclose(file);
return auxDIBImageLoad(filename);
}

return NULL;
}



int load_gl_textures()
{
AUX_RGBImageRec *TextureImage[2];

memset(TextureImage,0,sizeof(void *)*1);

TextureImage[0] = loadbmp("data\\tile.bmp");
TextureImage[1] = loadbmp("data\\tile2.bmp");

glGenTextures(2, &texture[0]); // We are generating two 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);
}


int draw_tiles(GLvoid)
{
int tile;
int y = 0;
for (int x=0; x< MAP_SIZEX; x++)
{

for (int y = 0; y < MAP_SIZEY; y++)
{

tile = map[y][x];
glBindTexture(GL_TEXTURE_2D, texture[tile]);

glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(float(x), -float(y + 1), 0.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(float(x + 1), -float(y + 1), 0.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(float(x + 1), -float(y), 0.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(float(x), -float(y), 0.0f);
glEnd();

}
}

return (1);
}

int InitGL(GLvoid) // All Setup For OpenGL Goes Here

{

if(!load_gl_textures())
{
return FALSE;
}
glShadeModel(GL_SMOOTH); // Enable Smooth Shading

glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background

glClearDepth(1.0f); // Depth Buffer Setup

glEnable(GL_DEPTH_TEST); // Enables Depth Testing

glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do

glEnable(GL_TEXTURE_2D);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations

return TRUE; // Initialization Went OK

}

int DrawGLScene(GLvoid) // Here''s Where We Do All The Drawing

{
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);

glTranslatef(-8.0f,6.0f,-14.5);// Move Left 1.5 Units And Into The Screen 29.0

draw_tiles();
return TRUE;
}

GLvoid KillGLWindow(GLvoid) // Properly Kill The Window

{
if (fullscreen) // Are We In Fullscreen Mode?

{
ChangeDisplaySettings(NULL,0); // If So Switch Back To The Desktop

ShowCursor(TRUE); // Show Mouse Pointer

}

if (hRC) // Do We Have A Rendering Context?

{
if (!wglMakeCurrent(NULL,NULL)) // Are We Able To Release The DC And RC Contexts?

{
MessageBox(NULL,"Release Of DC And RC Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
}

if (!wglDeleteContext(hRC)) // Are We Able To Delete The RC?

{
MessageBox(NULL,"Release Rendering Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
}
hRC=NULL; // Set RC To NULL

}

if (hDC && !ReleaseDC(hWnd,hDC)) // Are We Able To Release The DC

{
MessageBox(NULL,"Release Device Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
hDC=NULL; // Set DC To NULL

}

if (hWnd && !DestroyWindow(hWnd)) // Are We Able To Destroy The Window?

{
MessageBox(NULL,"Could Not Release hWnd.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
hWnd=NULL; // Set hWnd To NULL

}

if (!UnregisterClass("OpenGL",hInstance)) // Are We Able To Unregister Class

{
MessageBox(NULL,"Could Not Unregister Class.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
hInstance=NULL; // Set hInstance To NULL

}
}

/* This Code Creates Our OpenGL Window. Parameters Are: *
* title - Title To Appear At The Top Of The Window *
* width - Width Of The GL Window Or Fullscreen Mode *
* height - Height Of The GL Window Or Fullscreen Mode *
* bits - Number Of Bits To Use For Color (8/16/24/32) *
* fullscreenflag - Use Fullscreen Mode (TRUE) Or Windowed Mode (FALSE) */


BOOL CreateGLWindow(char* title, int width, int height, int bits, bool fullscreenflag)
{
GLuint PixelFormat; // Holds The Results After Searching For A Match

WNDCLASS wc; // Windows Class Structure

DWORD dwExStyle; // Window Extended Style

DWORD dwStyle; // Window Style

RECT WindowRect; // Grabs Rectangle Upper Left / Lower Right Values

WindowRect.left=(long)0; // Set Left Value To 0

WindowRect.right=(long)width; // Set Right Value To Requested Width

WindowRect.top=(long)0; // Set Top Value To 0

WindowRect.bottom=(long)height; // Set Bottom Value To Requested Height


fullscreen=fullscreenflag; // Set The Global Fullscreen Flag


hInstance = GetModuleHandle(NULL); // Grab An Instance For Our Window

wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; // Redraw On Size, And Own DC For Window.

wc.lpfnWndProc = (WNDPROC) WndProc; // WndProc Handles Messages

wc.cbClsExtra = 0; // No Extra Window Data

wc.cbWndExtra = 0; // No Extra Window Data

wc.hInstance = hInstance; // Set The Instance

wc.hIcon = LoadIcon(NULL, IDI_WINLOGO); // Load The Default Icon

wc.hCursor = LoadCursor(NULL, IDC_ARROW); // Load The Arrow Pointer

wc.hbrBackground = NULL; // No Background Required For GL

wc.lpszMenuName = NULL; // We Don''t Want A Menu

wc.lpszClassName = "OpenGL"; // Set The Class Name


if (!RegisterClass(&wc)) // Attempt To Register The Window Class

{
MessageBox(NULL,"Failed To Register The Window Class.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE

}

if (fullscreen) // Attempt Fullscreen Mode?

{
DEVMODE dmScreenSettings; // Device Mode

memset(&dmScreenSettings,0,sizeof(dmScreenSettings)); // Makes Sure Memory''s Cleared

dmScreenSettings.dmSize=sizeof(dmScreenSettings); // Size Of The Devmode Structure

dmScreenSettings.dmPelsWidth = width; // Selected Screen Width

dmScreenSettings.dmPelsHeight = height; // Selected Screen Height

dmScreenSettings.dmBitsPerPel = bits; // Selected Bits Per Pixel

dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;

// Try To Set Selected Mode And Get Results. NOTE: CDS_FULLSCREEN Gets Rid Of Start Bar.

if (ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL)
{
// If The Mode Fails, Offer Two Options. Quit Or Use Windowed Mode.

if (MessageBox(NULL,"The Requested Fullscreen Mode Is Not Supported By\nYour Video Card. Use Windowed Mode Instead?","NeHe GL",MB_YESNO|MB_ICONEXCLAMATION)==IDYES)
{
fullscreen=FALSE; // Windowed Mode Selected. Fullscreen = FALSE

}
else
{
// Pop Up A Message Box Letting User Know The Program Is Closing.

MessageBox(NULL,"Program Will Now Close.","ERROR",MB_OK|MB_ICONSTOP);
return FALSE; // Return FALSE

}
}
}

if (fullscreen) // Are We Still In Fullscreen Mode?

{
dwExStyle=WS_EX_APPWINDOW; // Window Extended Style

dwStyle=WS_POPUP; // Windows Style

ShowCursor(FALSE); // Hide Mouse Pointer

}
else
{
dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; // Window Extended Style

dwStyle=WS_OVERLAPPEDWINDOW; // Windows Style

}

AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle); // Adjust Window To True Requested Size


// Create The Window

if (!(hWnd=CreateWindowEx( dwExStyle, // Extended Style For The Window

"OpenGL", // Class Name

title, // Window Title

dwStyle | // Defined Window Style

WS_CLIPSIBLINGS | // Required Window Style

WS_CLIPCHILDREN, // Required Window Style

0, 0, // Window Position

WindowRect.right-WindowRect.left, // Calculate Window Width

WindowRect.bottom-WindowRect.top, // Calculate Window Height

NULL, // No Parent Window

NULL, // No Menu

hInstance, // Instance

NULL))) // Dont Pass Anything To WM_CREATE

{
KillGLWindow(); // Reset The Display

MessageBox(NULL,"Window Creation Error.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE

}

static PIXELFORMATDESCRIPTOR pfd= // pfd Tells Windows How We Want Things To Be

{
sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor

1, // Version Number

PFD_DRAW_TO_WINDOW | // Format Must Support Window

PFD_SUPPORT_OPENGL | // Format Must Support OpenGL

PFD_DOUBLEBUFFER, // Must Support Double Buffering

PFD_TYPE_RGBA, // Request An RGBA Format

bits, // Select Our Color Depth

0, 0, 0, 0, 0, 0, // Color Bits Ignored

0, // No Alpha Buffer

0, // Shift Bit Ignored

0, // No Accumulation Buffer

0, 0, 0, 0, // Accumulation Bits Ignored

16, // 16Bit Z-Buffer (Depth Buffer)

0, // No Stencil Buffer

0, // No Auxiliary Buffer

PFD_MAIN_PLANE, // Main Drawing Layer

0, // Reserved

0, 0, 0 // Layer Masks Ignored

};

if (!(hDC=GetDC(hWnd))) // Did We Get A Device Context?

{
KillGLWindow(); // Reset The Display

MessageBox(NULL,"Can''t Create A GL Device Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE

}

if (!(PixelFormat=ChoosePixelFormat(hDC,&pfd))) // Did Windows Find A Matching Pixel Format?

{
KillGLWindow(); // Reset The Display

MessageBox(NULL,"Can''t Find A Suitable PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE

}

if(!SetPixelFormat(hDC,PixelFormat,&pfd)) // Are We Able To Set The Pixel Format?

{
KillGLWindow(); // Reset The Display

MessageBox(NULL,"Can''t Set The PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE

}

if (!(hRC=wglCreateContext(hDC))) // Are We Able To Get A Rendering Context?

{
KillGLWindow(); // Reset The Display

MessageBox(NULL,"Can''t Create A GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE

}

if(!wglMakeCurrent(hDC,hRC)) // Try To Activate The Rendering Context

{
KillGLWindow(); // Reset The Display

MessageBox(NULL,"Can''t Activate The GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE

}

ShowWindow(hWnd,SW_SHOW); // Show The Window

SetForegroundWindow(hWnd); // Slightly Higher Priority

SetFocus(hWnd); // Sets Keyboard Focus To The Window

ReSizeGLScene(width, height); // Set Up Our Perspective GL Screen


if (!InitGL()) // Initialize Our Newly Created GL Window

{
KillGLWindow(); // Reset The Display

MessageBox(NULL,"Initialization Failed.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE

}

return TRUE; // Success

}

LRESULT CALLBACK WndProc( HWND hWnd, // Handle For This Window

UINT uMsg, // Message For This Window

WPARAM wParam, // Additional Message Information

LPARAM lParam) // Additional Message Information

{
switch (uMsg) // Check For Windows Messages

{
case WM_ACTIVATE: // Watch For Window Activate Message

{
if (!HIWORD(wParam)) // Check Minimization State

{
active=TRUE; // Program Is Active

}
else
{
active=FALSE; // Program Is No Longer Active

}

return 0; // Return To The Message Loop

}

case WM_SYSCOMMAND: // Intercept System Commands

{
switch (wParam) // Check System Calls

{
case SC_SCREENSAVE: // Screensaver Trying To Start?

case SC_MONITORPOWER: // Monitor Trying To Enter Powersave?

return 0; // Prevent From Happening

}
break; // Exit

}

case WM_CLOSE: // Did We Receive A Close Message?

{
PostQuitMessage(0); // Send A Quit Message

return 0; // Jump Back

}

case WM_KEYDOWN: // Is A Key Being Held Down?

{
keys[wParam] = TRUE; // If So, Mark It As TRUE

return 0; // Jump Back

}

case WM_KEYUP: // Has A Key Been Released?

{
keys[wParam] = FALSE; // If So, Mark It As FALSE

return 0; // Jump Back

}

case WM_SIZE: // Resize The OpenGL Window

{
ReSizeGLScene(LOWORD(lParam),HIWORD(lParam)); // LoWord=Width, HiWord=Height

return 0; // Jump Back

}
}

// Pass All Unhandled Messages To DefWindowProc

return DefWindowProc(hWnd,uMsg,wParam,lParam);
}

int WINAPI WinMain( HINSTANCE hInstance, // Instance

HINSTANCE hPrevInstance, // Previous Instance

LPSTR lpCmdLine, // Command Line Parameters

int nCmdShow) // Window Show State

{
MSG msg; // Windows Message Structure

BOOL done=FALSE; // Bool Variable To Exit Loop


// Ask The User Which Screen Mode They Prefer

if (MessageBox(NULL,"Would You Like To Run In Fullscreen Mode?", "Start FullScreen?",MB_YESNO|MB_ICONQUESTION)==IDNO)
{
fullscreen=FALSE; // Windowed Mode

}

// Create Our OpenGL Window

if (!CreateGLWindow("tarun''s tile engine",640,480,16,fullscreen))
{
return 0; // Quit If Window Was Not Created

}

while(!done) // Loop That Runs While done=FALSE

{
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) // Is There A Message Waiting?

{
if (msg.message==WM_QUIT) // Have We Received A Quit Message?

{
done=TRUE; // If So done=TRUE

}
else // If Not, Deal With Window Messages

{
TranslateMessage(&msg); // Translate The Message

DispatchMessage(&msg); // Dispatch The Message

}
}
else // If There Are No Messages

{
// Draw The Scene. Watch For ESC Key And Quit Messages From DrawGLScene()

if (active) // Program Active?

{
if (keys[VK_ESCAPE]) // Was ESC Pressed?

{
done=TRUE; // ESC Signalled A Quit

}
else // Not Time To Quit, Update Screen

{
DrawGLScene(); // Draw The Scene

SwapBuffers(hDC); // Swap Buffers (Double Buffering)

}
}

if (keys[VK_F1]) // Is F1 Being Pressed?

{
keys[VK_F1]=FALSE; // If So Make Key FALSE

KillGLWindow(); // Kill Our Current Window

fullscreen=!fullscreen; // Toggle Fullscreen / Windowed Mode

// Recreate Our OpenGL Window

if (!CreateGLWindow("tarun''s tile engine",640,480,16,fullscreen))
{
return 0; // Quit If Window Was Not Created

}
}
if (keys[VK_UP])
{
tarun+=.1;
}
if (keys[VK_DOWN])
{
tarun-=.1;
}

}
}

// Shutdown

KillGLWindow(); // Kill The Window

return (msg.wParam); // Exit The Program

}

don''t ask what I did - I don''t exactly know
All i know is that i switched glTranslate''s y from -6 to 6, and added some negative signs in the y''s in the loop.

Share this post


Link to post
Share on other sites
Very simple mistake with the mirroring.
What was happenning was you were starting at the bottom left of the map, drawing right and up. What you wanted to do was start at the top left, drawing right and down (like the array does).
The reason it works now is by increasing the "y" you actually end up going down , because it''s negative.

------------
http://aud.vze.com <-- Newbie alert, look at your own risk. Can induce severe laughing fits and other variations of hysterical outburst.

Share this post


Link to post
Share on other sites
Yeah, I had that logic in my head, I wasn't sure how to fix it in code :D

Hmm. One more question. I'm now working off of this code:
Now, before I draw my tiles, I want to draw a backdrop, so in case there is no tile there, the background will show through. I have loaded a 256x256 texture to stretch across the quad. the problem is, when I compile, all I get is a full screen of the tile stored in textures[1]. Actually, the background flashes for a second, and then it goes into the tiles. Anybody know what is wrong?

EDIT: never mind, i had my bind texture function commented out :D

[edited by - tarun713 on May 30, 2002 9:01:19 PM]

Share this post


Link to post
Share on other sites

  • Advertisement