file reader map generator

So my friend and I are making an action adventure game, and most things are working smoothly. The one thing that is hindering progress is our map loader however. It is designed to take a user generated map and translate that into a map. Sounds simple, right? The problem is that even though it works, it slows the frame rate to around 1-5 fps. I believe the problem is the nested for loops that we have, but neither of us can think of a work around. Can someone help us to get the fps up?

ps. If some code is missing just let me know, I might have missed some when going through the files.

Oh and we are using sdl :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//from file_reader.h

#ifndef FILEREADER_H
#define FILEREADER_H

#include "globals.h"

class FileReader {
	public:
		// class constructor
		FileReader(const char *filename);

		// class destructor
		~FileReader();

		//Map of integers
		int **map;
};

#endif // FILEREADER_H


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
61
62
//from file_reader.cpp

#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <exception>
#include <fstream>
#include <string>
#include "headers/globals.h"
#include "headers/file_reader.h" // class's header file

using namespace std;

int *row;

// class constructor
FileReader::FileReader(const char *filename) {
	ifstream fstr(filename);
	string str;

	//Read first line
	getline(fstr, str);
	MAP_HEIGHT = atoi(str.substr(8, str.size() - 8).c_str());

	//Read second line
	getline(fstr, str);
	MAP_WIDTH = atoi(str.substr(7, str.size() - 7).c_str());

	//Init pointer array
	try {
		row = new int[MAP_WIDTH];
		map = new int*[MAP_HEIGHT];
		for(int i = 0; i < MAP_HEIGHT; i++) {
			map[i] = new int[MAP_WIDTH];
		}
	} catch(exception &e) {
		cout << "Standard Error: " << e.what() << endl;
		system("pause");
	}

	//Read subsequent lines
	for(int i = 0; i < MAP_HEIGHT; i++) {
		getline(fstr, str);
		for(int j = 0; j < MAP_WIDTH; j++) {
			row[j] = atoi(str.substr(j, 1).c_str());
		}
		for(int j = 0; j < MAP_WIDTH; j++) {
			map[i][j] = row[j];
		}
	}
	fstr.close();
	delete[] row;
}

// class destructor
FileReader::~FileReader() {
	for(int i = 0; i > MAP_HEIGHT; i++) {
		delete[] map[i];
	}
	delete[] *map;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
//a snip from our init function
fileReader = new FileReader("maze.txt");
    map = new SDL_Surface**[MAP_HEIGHT];

    for(int i = 0; i < MAP_HEIGHT; i++) {
        map[i] = new SDL_Surface*[MAP_WIDTH];
    }

    for(int i = 0; i < MAP_WIDTH; i++) {
        for(int j = 0; j < MAP_HEIGHT; j++) {
            map[i][j] = NULL;
        }
    }


1
2
3
4
5
6
7
8
9
//our uninit function that deletes the map once the main loop ends

void uninit() {
    for(int i = 0; i < MAP_HEIGHT; i++) {
        delete[] *map[i];
    }
    delete[] *map;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//the actual function that draws the map

void drawMap(int **intMap) {

    for(int i = 0; i < MAP_WIDTH; i++) {
        for(int j = 0; j < MAP_HEIGHT; j++) {
            switch(intMap[j][i]) {
            case 0:
                map[i][j] = load_image("Images/testBlock1.png");
                apply_surface( (i * 16 ), ( j * 16 ), map[i][j], screen );
                break;
            case 1:
                map[i][j] = load_image("Images/testBlock2.png");
                apply_surface( (i * 16 ), ( j * 16 ), map[i][j], screen );
                break;
            default:
                break;
            }
        }
    }
}


and then we call drawMap(fileReader->map); in our main loop

the file it reads is:

Height: 10
Width: 10
0110000000000000000000000000000000000000
0110000000000000000000000000000000000000
0110000000000000000000000000000000000000
0110000000000000000000000000000000000000
0110000000000000000000000000000000000000
0110000000000000000000000000000000000000
0110000000000000000000000000000000000000
0110000000000000000000000000000000000000
0110000000000000000000000000000000000000
0110000000000000000000000000000000000000
0111111111111111111111111111111111111111
0111111111111111111111111111111111111111
0000000000000000000000000000000000000000

and that should be it! If you need more info, just ask.
Last edited on
Loading surfaces from file is slow so don't do it each frame. Instead, load each image file once and then reuse the same surface for everything that needs it.
Topic archived. No new replies allowed.