Advertisement
Guest User

gra

a guest
May 2nd, 2017
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.15 KB | None | 0 0
  1. //main.cpp
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. #include "SDL.h"
  6. #include "SDL_image.h"
  7.  
  8. #include "Room.h"
  9. #include "Input.h"
  10.  
  11. #define fps 60
  12. #define speed 5
  13.  
  14. #define tile_size 50
  15. #define rows 100
  16. #define columns 100
  17. #define map_width rows*tile_size
  18. #define map_height columns*tile_size
  19.  
  20. int main(int argc, char* args[])
  21. {
  22.     SDL_Init(SDL_INIT_EVERYTHING);
  23.     IMG_Init(IMG_INIT_PNG);
  24.     srand(time(NULL));
  25.  
  26.     int x0 = 0, y0 = 0, window_width = 1200, window_height = 700;
  27.     bool run = true;
  28.  
  29.     SDL_Window* win = SDL_CreateWindow("Game", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, window_width, window_height, SDL_WINDOW_RESIZABLE);
  30.     SDL_Surface *surf = NULL;
  31.     SDL_Event event;
  32.     Input input;
  33.     Uint32 time;
  34.     Map map(rows, columns, tile_size);
  35.    
  36.     while (run)
  37.     {
  38.         time = SDL_GetTicks();
  39.         while (SDL_PollEvent(&event))
  40.             if (event.type == SDL_QUIT)
  41.                 {
  42.                     run = false;
  43.                     break;
  44.                 }
  45.  
  46.         surf = SDL_GetWindowSurface(win);
  47.         SDL_GetWindowSize(win, &window_width, &window_height);
  48.  
  49.         if (input.isKeyDown(SDL_SCANCODE_UP) && y0 > 0)
  50.             y0 -= speed;
  51.         if (input.isKeyDown(SDL_SCANCODE_DOWN) && y0 < map_height - window_height)
  52.             y0 += speed;
  53.         if (input.isKeyDown(SDL_SCANCODE_LEFT) && x0 > 0)
  54.             x0 -= speed;
  55.         if (input.isKeyDown(SDL_SCANCODE_RIGHT) && x0 < map_width - window_width)
  56.             x0 += speed;
  57.  
  58.         map.draw(surf, x0, y0, window_width, window_height);
  59.         SDL_UpdateWindowSurface(win);
  60.  
  61.         if ((1000 / fps) > (SDL_GetTicks() - time))
  62.             SDL_Delay(1000 / fps - ((SDL_GetTicks() - time)));
  63.     }
  64.  
  65.     SDL_FreeSurface(surf);
  66.     SDL_DestroyWindow(win);
  67.     SDL_Quit();
  68.     return 0;
  69. }
  70. //_____________________________________________________________________________________
  71. //Room.h
  72. #pragma once
  73. #include <vector>
  74.  
  75. #include "Tile.h"
  76.  
  77. using namespace std;
  78.  
  79. class Room
  80. {
  81.     int x, y, w, h;
  82. public:
  83.     Room(int x = 0, int y = 0, int w = 0, int h = 0);
  84.     bool intersects(Room r);
  85.     int _x() { return x; }
  86.     int _y() { return y; }
  87.     int _w() { return w; }
  88.     int _h() { return h; }
  89. };
  90.  
  91. //------------
  92.  
  93. class Map
  94. {
  95.     vector<vector<Tile>> map;
  96.     vector<Room> rooms;
  97.     int rows, columns, tile_size;
  98. public:
  99.     Map(int rows, int columns, int tile_size);
  100.     void draw(SDL_Surface* dest, int x0, int y0, int w0, int h0);
  101.     void build();
  102. };
  103. //_____________________________________________________________________________________
  104. //Room.cpp
  105. #include <stdlib.h>
  106. #include <time.h>
  107.  
  108. #include "SDL_image.h"
  109.  
  110. #include "Room.h"
  111.  
  112. #define room_width_MAX 8
  113. #define room_width_MIN 3
  114. #define room_height_MAX 8
  115. #define room_height_MIN 3
  116. #define rooms_MAX 100
  117. #define rooms_MIN 1
  118.  
  119.  
  120. Room::Room(int x, int y, int w, int h) : x(x), y(y), w(w), h(h) {}
  121.  
  122. bool Room::intersects(Room r)
  123. {
  124.     return x <= (r._x() + r._w() - 1) && (x + w - 1) >= r._x() && y <= (r._y() + r._h() - 1) && (y + h - 1) >= r._y();
  125. }
  126.  
  127. //-----------
  128.  
  129. Map::Map(int rows, int columns, int tile_size) :rows(rows), columns(columns), tile_size(tile_size)
  130. {
  131.     for (int i = 0; i < rows; i++)
  132.     {
  133.         map.push_back(vector<Tile>());
  134.         for (int j = 0; j < columns; j++)
  135.             map[i].push_back(Tile(tile_size, tile_size));
  136.     }
  137.     build();
  138. }
  139.  
  140. void Map::draw(SDL_Surface* dest, int x0, int y0, int w0, int h0)
  141. {
  142.     for (int i = 0; i < rows; i++)
  143.         for (int j = 0; j < columns; j++)
  144.         {
  145.             int x1 = i * 50, y1 = j * 50;
  146.             if ((x1 + 50 > x0 && x1 < x0 + w0) && (y1 + 50 > y0 && y1 < y0 + h0))
  147.                 map[i][j].draw(dest, x1-x0, y1-y0);
  148.         }
  149. }
  150.  
  151. void Map::build()
  152. {
  153.     int n_rooms = rooms_MIN + rand() % (rooms_MAX-rooms_MIN+1), i=0;
  154.     bool intersects;
  155.  
  156.     while (i != n_rooms)
  157.     {
  158.         intersects = false;
  159.         Room new_room(rand() % rows, rand() % columns, room_width_MIN + rand() % (room_width_MAX - room_width_MIN + 1), room_height_MIN + rand() % (room_height_MAX - room_height_MIN + 1));
  160.         if (new_room._x() + new_room._w() - 1 >= rows || new_room._y() + new_room._h() - 1 >= columns)  continue;
  161.         for(int j=0; j<i; j++)
  162.             if (new_room.intersects(rooms[j]))
  163.             {
  164.                 intersects = true;
  165.                 break;
  166.             }
  167.         if (intersects) continue;
  168.         else
  169.         {
  170.             rooms.push_back(new_room);
  171.             i++;
  172.         }
  173.     }
  174.     for (int i = 0; i < n_rooms; i++)
  175.     {
  176.         for (int j = rooms[i]._x(); j < rooms[i]._x() + rooms[i]._w() - 1; j++)
  177.             for (int k = rooms[i]._y(); k < rooms[i]._y() + rooms[i]._h() - 1; k++)
  178.                 map[j][k].type = ROOM;
  179.     }
  180.  
  181. }
  182. //_____________________________________________________________________________________
  183. //Tile.h
  184. #pragma once
  185. #include "SDL.h"
  186.  
  187. enum tile_type { GROUND, ROOM };
  188.  
  189. class Tile
  190. {
  191.     SDL_Rect place;
  192. public:
  193.     Tile(int w, int h, tile_type type = GROUND);
  194.     tile_type type;
  195.     void draw(SDL_Surface* dest, int x, int y);
  196. };
  197. //_____________________________________________________________________________________
  198. //Tile.cpp
  199. #include "Tile.h"
  200. #include "SDL_image.h"
  201.  
  202. SDL_Texture *img_ground = IMG_Load("ground.bmp"),
  203.             *img_room = IMG_Load("room.bmp");
  204.  
  205. Tile::Tile(int w, int h, tile_type type):type(type)
  206. {
  207.     place.w = w;
  208.     place.h = h;
  209. }
  210.  
  211. void Tile::draw(SDL_Surface* dest, int x, int y)
  212. {
  213.     place.x = x;
  214.     place.y = y;
  215.     switch (type)
  216.     {
  217.         case GROUND:
  218.             SDL_BlitSurface(img_ground, NULL, dest, &place);
  219.             break;
  220.         case ROOM:
  221.             SDL_BlitSurface(img_room, NULL, dest, &place);
  222.             break;
  223.     }
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement