Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //main.cpp
- #include <stdlib.h>
- #include <time.h>
- #include "SDL.h"
- #include "SDL_image.h"
- #include "Room.h"
- #include "Input.h"
- #define fps 60
- #define speed 5
- #define tile_size 50
- #define rows 100
- #define columns 100
- #define map_width rows*tile_size
- #define map_height columns*tile_size
- int main(int argc, char* args[])
- {
- SDL_Init(SDL_INIT_EVERYTHING);
- IMG_Init(IMG_INIT_PNG);
- srand(time(NULL));
- int x0 = 0, y0 = 0, window_width = 1200, window_height = 700;
- bool run = true;
- SDL_Window* win = SDL_CreateWindow("Game", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, window_width, window_height, SDL_WINDOW_RESIZABLE);
- SDL_Surface *surf = NULL;
- SDL_Event event;
- Input input;
- Uint32 time;
- Map map(rows, columns, tile_size);
- while (run)
- {
- time = SDL_GetTicks();
- while (SDL_PollEvent(&event))
- if (event.type == SDL_QUIT)
- {
- run = false;
- break;
- }
- surf = SDL_GetWindowSurface(win);
- SDL_GetWindowSize(win, &window_width, &window_height);
- if (input.isKeyDown(SDL_SCANCODE_UP) && y0 > 0)
- y0 -= speed;
- if (input.isKeyDown(SDL_SCANCODE_DOWN) && y0 < map_height - window_height)
- y0 += speed;
- if (input.isKeyDown(SDL_SCANCODE_LEFT) && x0 > 0)
- x0 -= speed;
- if (input.isKeyDown(SDL_SCANCODE_RIGHT) && x0 < map_width - window_width)
- x0 += speed;
- map.draw(surf, x0, y0, window_width, window_height);
- SDL_UpdateWindowSurface(win);
- if ((1000 / fps) > (SDL_GetTicks() - time))
- SDL_Delay(1000 / fps - ((SDL_GetTicks() - time)));
- }
- SDL_FreeSurface(surf);
- SDL_DestroyWindow(win);
- SDL_Quit();
- return 0;
- }
- //_____________________________________________________________________________________
- //Room.h
- #pragma once
- #include <vector>
- #include "Tile.h"
- using namespace std;
- class Room
- {
- int x, y, w, h;
- public:
- Room(int x = 0, int y = 0, int w = 0, int h = 0);
- bool intersects(Room r);
- int _x() { return x; }
- int _y() { return y; }
- int _w() { return w; }
- int _h() { return h; }
- };
- //------------
- class Map
- {
- vector<vector<Tile>> map;
- vector<Room> rooms;
- int rows, columns, tile_size;
- public:
- Map(int rows, int columns, int tile_size);
- void draw(SDL_Surface* dest, int x0, int y0, int w0, int h0);
- void build();
- };
- //_____________________________________________________________________________________
- //Room.cpp
- #include <stdlib.h>
- #include <time.h>
- #include "SDL_image.h"
- #include "Room.h"
- #define room_width_MAX 8
- #define room_width_MIN 3
- #define room_height_MAX 8
- #define room_height_MIN 3
- #define rooms_MAX 100
- #define rooms_MIN 1
- Room::Room(int x, int y, int w, int h) : x(x), y(y), w(w), h(h) {}
- bool Room::intersects(Room r)
- {
- return x <= (r._x() + r._w() - 1) && (x + w - 1) >= r._x() && y <= (r._y() + r._h() - 1) && (y + h - 1) >= r._y();
- }
- //-----------
- Map::Map(int rows, int columns, int tile_size) :rows(rows), columns(columns), tile_size(tile_size)
- {
- for (int i = 0; i < rows; i++)
- {
- map.push_back(vector<Tile>());
- for (int j = 0; j < columns; j++)
- map[i].push_back(Tile(tile_size, tile_size));
- }
- build();
- }
- void Map::draw(SDL_Surface* dest, int x0, int y0, int w0, int h0)
- {
- for (int i = 0; i < rows; i++)
- for (int j = 0; j < columns; j++)
- {
- int x1 = i * 50, y1 = j * 50;
- if ((x1 + 50 > x0 && x1 < x0 + w0) && (y1 + 50 > y0 && y1 < y0 + h0))
- map[i][j].draw(dest, x1-x0, y1-y0);
- }
- }
- void Map::build()
- {
- int n_rooms = rooms_MIN + rand() % (rooms_MAX-rooms_MIN+1), i=0;
- bool intersects;
- while (i != n_rooms)
- {
- intersects = false;
- 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));
- if (new_room._x() + new_room._w() - 1 >= rows || new_room._y() + new_room._h() - 1 >= columns) continue;
- for(int j=0; j<i; j++)
- if (new_room.intersects(rooms[j]))
- {
- intersects = true;
- break;
- }
- if (intersects) continue;
- else
- {
- rooms.push_back(new_room);
- i++;
- }
- }
- for (int i = 0; i < n_rooms; i++)
- {
- for (int j = rooms[i]._x(); j < rooms[i]._x() + rooms[i]._w() - 1; j++)
- for (int k = rooms[i]._y(); k < rooms[i]._y() + rooms[i]._h() - 1; k++)
- map[j][k].type = ROOM;
- }
- }
- //_____________________________________________________________________________________
- //Tile.h
- #pragma once
- #include "SDL.h"
- enum tile_type { GROUND, ROOM };
- class Tile
- {
- SDL_Rect place;
- public:
- Tile(int w, int h, tile_type type = GROUND);
- tile_type type;
- void draw(SDL_Surface* dest, int x, int y);
- };
- //_____________________________________________________________________________________
- //Tile.cpp
- #include "Tile.h"
- #include "SDL_image.h"
- SDL_Texture *img_ground = IMG_Load("ground.bmp"),
- *img_room = IMG_Load("room.bmp");
- Tile::Tile(int w, int h, tile_type type):type(type)
- {
- place.w = w;
- place.h = h;
- }
- void Tile::draw(SDL_Surface* dest, int x, int y)
- {
- place.x = x;
- place.y = y;
- switch (type)
- {
- case GROUND:
- SDL_BlitSurface(img_ground, NULL, dest, &place);
- break;
- case ROOM:
- SDL_BlitSurface(img_room, NULL, dest, &place);
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement