-
Notifications
You must be signed in to change notification settings - Fork 0
/
game_map.cpp
98 lines (80 loc) · 2.11 KB
/
game_map.cpp
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include<stdio.h>
#include "game_map.h"
void GameMap::LoadMap(char* name)
{
FILE* fp = fopen(name, "rb");
if(!fp)
{
return ;
}
game_map_.max_x_ = 0;
game_map_.max_y_ = 0;
for(int i=0;i < MAX_MAP_Y;i++)
{
for(int j =0; j<MAX_MAP_X;j++)
{
fscanf(fp,"%d", &game_map_.tile[i][j]);
int val = game_map_.tile[i][j];
if(val > 0)
{
if(j > game_map_.max_x_)
{
game_map_.max_x_ = j;
}
if(i > game_map_.max_y_)
{
game_map_.max_y_ = i;
}
}
}
}
game_map_.max_x_ = (game_map_.max_x_ + 1)*TILE_SIZE;
game_map_.max_y_ = (game_map_.max_y_ + 1)*TILE_SIZE;
game_map_.start_x_ = 0;
game_map_.start_y_ = 0;
game_map_.file_name_ = name;
fclose(fp);
}
void GameMap::LoadTiles(SDL_Renderer*screen)
{
char file_img[30];
for(int i=0;i < MAX_TILES;i++)
{
sprintf(file_img,"map/%d.png",i);
FILE *fp = fopen(file_img,"rb");
if(fp == NULL)
continue;
fclose(fp);
tile_mat[i].LoadImg(file_img,screen);
}
}
void GameMap::DrawMap(SDL_Renderer*screen)
{
int x1 = 0;
int x2 = 0;
int y1 = 0;
int y2 = 0;
int map_x = 0;
int map_y = 0;
map_x = game_map_.start_x_/TILE_SIZE;
x1 = (game_map_.start_x_%TILE_SIZE)*-1;
x2 = x1 + SCREEN_WIDTH +(x1==0 ? 0 : TILE_SIZE);
map_y = game_map_.start_y_/TILE_SIZE;
y1 = (game_map_.start_y_%TILE_SIZE)*-1;
y2 = y1 + SCREEN_HEIGHT +(y1==0 ? 0 : TILE_SIZE);
for(int i= y1;i<y2;i+= TILE_SIZE)
{
map_x = game_map_.start_x_/TILE_SIZE;
for(int j=x1;j<x2;j += TILE_SIZE)
{
int val = game_map_.tile[map_y][map_x];
if (val > 0)
{
tile_mat[val].SetRect(j,i);
tile_mat[val].Render(screen);
}
map_x++;
}
map_y++;
}
}