My application stopps working beacuse map loader.

I have problems with my game engine.
I made a basic map loader that loads maps.

But its not loading maps its stopps working apps.

Here the code:
#pragma once
#pragma once
#include "pch.h"
#include <iostream>
#include <conio.h>
#include "glut.h"
#include <vector>
#include <math.h>
#include <windows.h>
#include <string>
#include <mmsystem.h>
#include <algorithm>

#include <fstream>
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
#include "models.h"
#include <vector>
int Mapc[30][30] = { //our map
{1,1,1,1},
{0,1,1,0},
{0,1,1,0},
{0,1,1,0},
{0,1,1,1}
};
void MapLoader(const char * fname,int Map [30][30])
{


ifstream in(fname, ios::in);
int dataholder;
int dx = 0;

while (in >> Map[30][dx++]);

in.close();
}

void MapGen(int Map[30][30])
{

for (int i = 0; i < 10; i++)
{

for (int j = 1; j < 10; j++)
{
if(Map[i][j] == 1) {
glPushMatrix();
glTranslatef(j, -i, 0);
drawBox();
glPopMatrix();
}
}
}
for (int i = 0; i < 10; i++)
{

for (int j = 0; j < 10; j++)
{
if (Map[i][j] == 0) {
glPushMatrix();

glPopMatrix();
}
}

}
}



You allocated space for a 30x30 array, but you're trying to access map[30], which is out of bounds.
Valid indices would be 0 through 29 in both dimensions.

Also, once (if) dx goes to 30, you are also going out of bounds of the inner array.

As an aside, if you're going to learn OpenGL, I should strongly suggest learning modern OpenGL. The stuff like glPush, glPop, glBegin, glEnd ("immediate mode" stuff) is deprecated.
This is a good site to learn modern OpenGL: http://www.opengl-tutorial.org/

___________________________________

Bigger picture: What is it you want in Map once it's completely loaded? What file is being loaded? Is the map a hardcoded 4x6 map, or do you want it to be a 30x30 map?


Here's an example of a way one might load a basic rectangular map into memory:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <string>
#include <fstream>
#include <vector>

using std::vector;

vector<vector<int>> load_map(const std::string& filename)
{
	std::ifstream fin(filename);
	int rows;
	int columns;
	
	if (!(fin >> rows >> columns))
	{
		std::cout << "ERROR: Cannot read map dimensions\n";
		return vector<vector<int>>(); // return empty map
	}
	
	// generate a 2D rows-by-columns-size vector,
	// cells initialized to 0
	vector<vector<int>> map(rows, vector<int>(columns, 0));
	
	for (int i = 0; i < rows; i++)
	{
		for (int j = 0; j < columns; j++)
		{
			int cell;
			if (fin >> cell)
			{
				map[i][j] = cell;
			}
			else
			{
				std::cout << "ERROR: Bad map format\n";
				return map;
			}
		}
	}
	
	return map;
}

void display_map(const vector<vector<int>>& map)
{
	for (const auto& row : map)
	{
		for (const int cell : row)
		{
			std::cout << cell << ' ';
		}
		std::cout << '\n';
	}
}

int main()
{
	vector<vector<int>> map = load_map("map.txt");
	display_map(map);
}


map.txt:
5 7
1 1 1 1 1 0 0
0 1 1 0 0 0 1
0 0 1 0 1 1 1
0 0 0 0 0 0 1
0 1 1 1 1 0 1

I use the first two numbers to signify the number of rows and columns, respectively.
Last edited on
Topic archived. No new replies allowed.