-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
149 lines (117 loc) · 5.13 KB
/
main.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <iostream>
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
#include <SDL.h>
#include <SDL_image.h>
#else
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#endif
#include <thread>
#include "Scenario/defs.hpp"
#include <Renderer/renderer_window.hpp>
#include <Scenario/wall_block.hpp>
#include <Scenario/map.hpp>
#include "IO/map_reader.hpp"
#include <Char/char.hpp>
#include <chrono>
#include <memory>
#include "Memory/sdl_deleter.hpp"
using namespace std;
using namespace std::literals::chrono_literals;
SDL_Renderer * RendererWindow::renderer = NULL;
#define NUMBER_OF_PLAYER_SPRITES 6
int main(int argc, char * argv[])
{
try
{
// read the map txt file
Map scene_map = readMap("../res/map1.txt");
if(SDL_Init(SDL_INIT_VIDEO) > 0)
throw runtime_error(SDL_GetError());
if(!IMG_Init(IMG_INIT_PNG))
throw runtime_error(SDL_GetError());
// create a render object to render the scenario and the objects
RendererWindow renderer_window("Bomber Man v1",960,480);
const int block_w = 960/scene_map.w;
const int block_h = 480/(scene_map.h);
renderer_window.setBlocksSize(block_w,block_h);
bool isRunning = true;
SDL_Event event;
//store the data from the wall blocks into a share_ptr to be placed on many places of the screen
std::shared_ptr<SDL_Texture> texture =
std::shared_ptr<SDL_Texture>(RendererWindow::loadTexture(WALL_IMAGE_PATH),SDL_Deleter());
//do the same with the ground blocks
std::shared_ptr<SDL_Texture> texture2 =
std::shared_ptr<SDL_Texture>(RendererWindow::loadTexture(GROUND_IMAGE_PATH),SDL_Deleter());
auto loadTexturesFromCharacter =
/**
* @brief Lambda function to read all the input images of the disk and store it into
*
* @param texture_folder_path
* @param walk_directions
*/
[](const std::string texture_folder_path,const std::vector<std::string> walk_directions,DirectionTextures & direction_textures)
{
int size = walk_directions.size();
const std::string suffix = ".jpg";
const std::string separator = "/";
CircularList <SDL_Texture*,SDL_Deleter> char_walk_textures[4];
// a walk_direction name is the direction that character is walking in a string
// e.g. back_walk, front_walk
int i = 0;
for(auto walk_direction:walk_directions)
{
for(int j=0;j<NUMBER_OF_PLAYER_SPRITES;j++)
{
std::string texture_filename = texture_folder_path+walk_direction + std::to_string(j+1)+ suffix;
SDL_Texture * ptr = RendererWindow::loadTexture(texture_filename.c_str());
char_walk_textures[i].push_back(ptr);
}
i++;
}
direction_textures.up = char_walk_textures[0].copy();
for(int i=0;i<NUMBER_OF_PLAYER_SPRITES;i++)
{
std::cout<<direction_textures.up.getNextData()<<"\n";
}
direction_textures.down = char_walk_textures[1].copy();
direction_textures.rigth = char_walk_textures[2].copy();
direction_textures.left = char_walk_textures[3].copy();
};
std::vector<std::string> walk_directions = {"back_walk","front_walk","right_walk","left_walk"};
DirectionTextures player_textures;
loadTexturesFromCharacter("../res/Chars/Bomberman/",walk_directions,player_textures);
Character p(30,30,block_w,block_h,player_textures);
WallBlock * wall_block = new WallBlock(0,0,block_w,block_h,texture.get());
GroundBlock * ground_block = new GroundBlock(0,0,block_w,block_h,texture2.get());
scene_map.dict_texture.insert(std::pair<char,RenderableObject*>(BLOCK_WALL,(RenderableObject*) wall_block));
scene_map.dict_texture.insert(std::pair<char,RenderableObject*>(BLOCK_GROUND,(RenderableObject*)ground_block));
while(isRunning)
{
renderer_window.clearScreen();
renderer_window.renderMap(scene_map);
renderer_window.render(p);
renderer_window.display();
if (SDL_PollEvent(&event) && event.type == SDL_QUIT)
{
isRunning = false;
}
switch( event.type ){
case SDL_KEYDOWN:
if( event.key.keysym.sym ){
p.move(event.key.keysym.sym);
event.key.keysym.sym = 0;
}
default:
break;
}
std::this_thread::sleep_for(1ms);
}
SDL_Quit();
}
catch (runtime_error e)
{
std::cout<<"we have a error\n"<<e.what()<<"\n";
}
return 0;
}