Dereferencing Problem

Started by
20 comments, last by Alpha_ProgDes 17 years, 9 months ago
for(int y = 0; y <= 100; ++y)                  ^                  |
deathkrushPS3/Xbox360 Graphics Programmer, Mass Media.Completed Projects: Stuntman Ignition (PS3), Saints Row 2 (PS3), Darksiders(PS3, 360)
Advertisement
Just to clean up your texture lookup a little bit (sorry I can't help with your problem):

GLuint TextureManager::GetTexture(char TextureID)
{
if ('0' > TextureID || '9'
this doesn't answer but just a suggestion to your GetTexture function:
GLuint TextureManager::GetTexture(char TextureID){    int textureNumID = TextureID - '0';    if (textureNumID >= 0 && textureNumID < 10)    {       return TerrainTextures[textureNumID];    }    else     {       MessageBox(NULL, "That's not a valid texture", "RPG", MB_ICONEXCLAMATION);       return NULL;    }}

Beginner in Game Development?  Read here. And read here.

 

Okay, it no longer sends an error at me, but the window does come up and disappear. And the debugger still gives me that error message. I fixed the array though.

[edit]
When the debugger error message comes, this line pops up.

MapArray[yIndex][xIndex].ID = MapFileLines[yIndex][xIndex];

Another thing it does, is if I get the debugger to run without having it do the above, it says the error, when I get to the line where I declare my Renderer.
Heres my WinMain.
#include <windows.h>#include <gl/gl.h>#include "Renderer.h"int WINAPI WinMain (HINSTANCE hInstance,                    HINSTANCE hPrevInstance,                    LPSTR lpCmdLine,                    int iCmdShow){    WNDCLASS wc;    HWND hWnd;    HDC hDC;    HGLRC hRC;            MSG msg;    BOOL bQuit = FALSE;    float theta = 0.0f;        wc.style = CS_OWNDC;    wc.lpfnWndProc = WndProc;    wc.cbClsExtra = 0;    wc.cbWndExtra = 0;    wc.hInstance = hInstance;    wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);    wc.hCursor = LoadCursor (NULL, IDC_ARROW);    wc.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);    wc.lpszMenuName = NULL;    wc.lpszClassName = "GLSample";    RegisterClass (&wc);    hWnd = CreateWindow (      "GLSample", "OpenGL Sample",       WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,      0, 0, 256, 256,      NULL, NULL, hInstance, NULL);            EnableOpenGL (hWnd, &hDC, &hRC);    Renderer MainRenderer;    while (!bQuit)    {        if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))        {            if (msg.message == WM_QUIT)            {                bQuit = TRUE;            }            else            {                TranslateMessage (&msg);                DispatchMessage (&msg);            }        }        else        {            MainRenderer.RenderMap();            SwapBuffers (hDC);            Sleep (1);        }    }    DisableOpenGL (hWnd, hDC, hRC);    DestroyWindow (hWnd);    return msg.wParam;}
you have to post the code for Renderer or at least RenderMap

Beginner in Game Development?  Read here. And read here.

 

Renderer.h
#ifndef RENDERER_H#define RENDERER_H#include "Map.h"#include <gl/gl.h>class Renderer{    private:        void RenderTile(float x, float y, GLuint Texture);    public:        Renderer();        Map TestMap;        void RenderMap();};    #endif

Renderer.cpp
#include "Renderer.h"#include <windows.h>Renderer::Renderer(){    glEnable(GL_TEXTURE_2D);    glShadeModel(GL_SMOOTH);    glClearDepth(1.0);    glEnable(GL_DEPTH_TEST);    glDepthFunc(GL_LEQUAL);    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);    glClearColor(0.0, 0.0, 0.0, 0.0);    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);    glLoadIdentity();}void Renderer::RenderTile(float x, float y, GLuint Texture){    static int count=0;    float outx, outy, outx2, outy2;    outx = (100.0 / (x * 16.0)) - 1.5;    outx2 = (100.0 / (x * 16.0)) - 1.4375;    outy = (100.0 / (y * 16.0)) - 1.5;    outy2 = (100.0 / (y * 16.0)) - 1.4375;    glBegin(GL_QUADS);        glBindTexture(GL_TEXTURE_2D, Texture);        glTexCoord2f(0.0, 0.0); glVertex2f(outx, outy);        glTexCoord2f(0.0, 1.0); glVertex2f(outx, outy2);        glTexCoord2f(1.0, 1.0); glVertex2f(outx2, outy2);        glTexCoord2f(1.0, 0.0); glVertex2f(outx2, outy);    glEnd();    count++;}void Renderer::RenderMap(){    for(int y = 0; y <= 100; ++y)    {        for(int x = 0; x <= 100; ++x)        {            RenderTile((float)x, (float)y, *TestMap.MapArray[x][y].TileSprite.TextureHandle);        }    }}
Post the code where the MapArray and MapFileLines are getting declared and initialized.
deathkrushPS3/Xbox360 Graphics Programmer, Mass Media.Completed Projects: Stuntman Ignition (PS3), Saints Row 2 (PS3), Darksiders(PS3, 360)
Map.h
#ifndef MAP_H#define MAP_H#include <string>#include "Tile.h"#include "TextureManager.h"class Map{    private:        std::string MapFileLines[100];        std::string FileLineBuffer;        TextureManager TexMan;        bool LoadMap();    public:        Tile MapArray[100][100];        Map();};    #endif

Map.cpp
#include "Map.h"#include <windows.h>#include <fstream>#include <iostream>bool Map::LoadMap(){    int line = 0;    std::ifstream MapFile("Map.txt");    if(!MapFile.is_open())    {        return false;    }    while(!MapFile.eof())    {        std::getline(MapFile, FileLineBuffer);        MapFileLines[line] = FileLineBuffer;        line++;    }    MapFile.close();    return true;}Map::Map(){    if(!LoadMap())        MessageBox(NULL, "The map was not loaded", "RPG", MB_ICONEXCLAMATION);    for(int yIndex = 0; yIndex <= 100; yIndex++)    {        for(int xIndex = 0; xIndex <= 100; xIndex++)        {            MapArray[yIndex][xIndex].ID = MapFileLines[yIndex][xIndex];            MapArray[yIndex][xIndex].CopyTextureTile(TexMan.GetTexture(MapArray[yIndex][xIndex].ID));        }    }}
I don't know if you changed it yet but
for (int y = 0; y <= 100; ++y) {  //.....}/* needs to be */for (int y = 0; y < 100; ++y) {  //.....}

for every loop that you have. the reason being is that you are declaring 100 elements and the index starts with 0 not 1. so you go from 0 - 99, which, if you count it correctly, is 100 elements.

edit: also when you are returning functions, especially with the files, you should accompany them with a MessageBox so you know which function failed. easier to debug that way also.

Beginner in Game Development?  Read here. And read here.

 

This looks kind of dangerous:

std::string MapFileLines[100];

Are you sure that every std::string inside MapFileLines is 100-characters long?
deathkrushPS3/Xbox360 Graphics Programmer, Mass Media.Completed Projects: Stuntman Ignition (PS3), Saints Row 2 (PS3), Darksiders(PS3, 360)

This topic is closed to new replies.

Advertisement