Skip to content
Snippets Groups Projects
Commit 50fec9e2 authored by sgupta72's avatar sgupta72
Browse files

Initial commit

parents
No related branches found
No related tags found
No related merge requests found
all :
g++ main.cpp game.cpp window.cpp rect.cpp -lSDL2 -lSDL2_image
Mario_Sprite.jpg

4.76 KiB

a.out 0 → 100755
File added
game.cpp 0 → 100644
#include "game.h"
#include "window.h"
#include "rect.h"
#include <iostream>
#include <SDL2/SDL_image.h>
using namespace std;
Game::Game()
{
if(SDL_Init(SDL_INIT_VIDEO) < 0)
{
cout << "Could not initialize SDL: " << SDL_GetError() << endl;
return;
}
cout << IMG_INIT_PNG << endl;
if(!IMG_Init(IMG_INIT_PNG))
{
cout << "Could not initialize SDL_image: " << IMG_GetError() << endl;
}
quit = false;
GameLoop();
}
void Game::GameLoop()
{
Window window;
Rect rect("mario.png", window);
vector<Rect*> rects;
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 9; j++)
{
rects.push_back(new Rect("spritesheet.png", window, 4, 9));
rects[rects.size()-1]->Move(300, (9 * i + j) * 64);
cout << "here " << i << " " << j << " " << rects.size() << endl;
}
}
while(!quit)
{
while(SDL_PollEvent(&e) != 0)
{
if(e.type == SDL_QUIT)
quit = true;
else if(e.type == SDL_KEYDOWN && e.key.repeat == 0)
{
switch (e.key.keysym.sym)
{
case SDLK_UP:
rect.AddVelocity(0, -10);
break;
case SDLK_DOWN:
rect.AddVelocity(0, 10);
break;
case SDLK_LEFT:
rect.AddVelocity(-10, 0);
break;
case SDLK_RIGHT:
rect.AddVelocity(10, 0);
break;
}
}
else if(e.type == SDL_KEYUP && e.key.repeat == 0)
{
switch (e.key.keysym.sym)
{
case SDLK_UP:
rect.AddVelocity(0, 10);
break;
case SDLK_DOWN:
rect.AddVelocity(0, -10);
break;
case SDLK_LEFT:
rect.AddVelocity(10, 0);
break;
case SDLK_RIGHT:
rect.AddVelocity(-10, 0);
break;
}
}
}
rect.UpdatePosition();
rect.CorrectPosition(rects);
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 9; j++)
{
cout << "HERE" << i << " " << j << " " << rects.size() << " " << 9 * i + j << endl;
rects[9 * i + j]->Draw(window, i, j);
}
}
rect.Draw(window);
window.Update();
SDL_Delay(100);
}
}
Game::~Game()
{
SDL_Quit();
}
game.h 0 → 100644
#ifndef GAME_H
#define GAME_H
#include <SDL2/SDL.h>
class Game
{
public:
Game();
~Game();
void GameLoop();
private:
bool quit;
SDL_Event e;
};
#endif
#include <SDL2/SDL.h>
#include <iostream>
#include "game.h"
using namespace std;
int main()
{
Game game;
}
main.o 0 → 100644
File added
mario.bmp 0 → 100644
mario.bmp

960 KiB

mario.png 0 → 100644
mario.png

7.13 KiB

rect.cpp 0 → 100644
#include "rect.h"
#include "window.h"
#include <string>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <iostream>
using namespace std;
void Rect::LoadImage(string filename, Window &window)
{
SDL_Surface *loaded = IMG_Load(filename.c_str());
if(loaded == NULL)
{
cout << "Unable to load image: " << filename << " " << IMG_GetError() << endl;
return;
}
image = SDL_ConvertSurface(loaded, window.get_screen_surface()->format, NULL);
if(image == NULL)
{
cout << "Unable to convert surface: " << SDL_GetError() << endl;
}
SDL_FreeSurface(loaded);
}
Rect::Rect(string filename, Window &window)
{
LoadImage(filename, window);
x = 0;
y = 0;
w = image->w;
h = image->h;
vx = 0;
vy = 0;
rows = 1;
columns = 1;
}
Rect::Rect(string filename, Window &window, int rows, int columns)
{
LoadImage(filename, window);
x = 0;
y = 0;
w = image->w/columns;
h = image->h/rows;
vx = 0;
vy = 0;
this->rows = rows;
this->columns = columns;
}
Rect::~Rect()
{
SDL_FreeSurface(image);
image = NULL;
}
void Rect::Move(int x, int y)
{
this->x = x;
this->y = y;
}
void Rect::AddVelocity(int vx, int vy)
{
this->vx += vx;
this->vy += vy;
}
void Rect::UpdatePosition()
{
x += vx;
y += vy;
}
void Rect::Draw(Window &window)
{
SDL_Rect dstrect;
dstrect.x = x;
dstrect.y = y;
SDL_BlitSurface(image, NULL, window.get_screen_surface(), &dstrect);
}
void Rect::Draw(Window &window, int row, int column)
{
SDL_Rect srcrect;
srcrect.x = (double(row)/double(rows)) * (image->h/rows);
srcrect.y = (double(column)/double(columns)) * (image->w / columns);
srcrect.w = image->w/columns;
srcrect.h = image->h/rows;
cout << srcrect.x << " " << srcrect.y << " " << srcrect.w << " " << srcrect.h << endl;
SDL_Rect dstrect;
dstrect.x = x;
dstrect.y = y;
SDL_BlitSurface(image, &srcrect, window.get_screen_surface(), &dstrect);
}
bool Rect::CheckCollision(Rect &rect)
{
if((y + h <= rect.y) || (rect.y + rect.h <= y))
return false;
if((x + w <= rect.x) || (rect.x + rect.w <= x))
return false;
return true;
}
void Rect::CorrectPosition(vector<Rect*> rects)
{
for(int i = 0; i < rects.size(); i++)
{
if(CheckCollision(*rects[i]))
{
x -= vx;
y -= vy;
break;
}
}
}
SDL_Surface *Rect::get_image()
{
return image;
}
rect.h 0 → 100644
#ifndef RECT_H
#define RECT_H
#include <string>
#include <SDL2/SDL.h>
#include <vector>
#include "window.h"
using namespace std;
/**
* Class to render rectangles and handle collision detection between rectangles
**/
class Window;
class Rect
{
public:
Rect(string filename, Window &window);
//load spritesheet
Rect(string filename, Window &window, int rows, int columns);
~Rect();
void Move(int x, int y);
void AddVelocity(int vx, int vy);
void UpdatePosition();
void Draw(Window &window);
void Draw(Window &window, int row, int column);
bool CheckCollision(Rect &rect);
void CorrectPosition(vector<Rect*> rects);
SDL_Surface *get_image();
private:
void LoadImage(string filename, Window &window);
SDL_Surface *image;
int x, y;
int w, h;
int vx, vy;
int rows, columns;
};
#endif
spritesheet.png

141 KiB

#include "window.h"
#include <SDL2/SDL.h>
#include <iostream>
#include "rect.h"
using namespace std;
Window::Window()
{
window = SDL_CreateWindow("Platformer", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600, SDL_WINDOW_SHOWN);
if(window == NULL)
{
cout << "Window could not be created: " << SDL_GetError() << endl;
return;
}
cout << "HERE" << endl;
screen_surface = SDL_GetWindowSurface(window);
SDL_FillRect(screen_surface, NULL, SDL_MapRGB(screen_surface->format, 0xFF, 0xFF, 0xFF));
SDL_UpdateWindowSurface(window);
}
Window::~Window()
{
SDL_DestroyWindow(window);
window = NULL;
}
SDL_Surface *Window::get_screen_surface()
{
return screen_surface;
}
void Window::Update()
{
SDL_UpdateWindowSurface(window);
}
window.h 0 → 100644
#ifndef WINDOW_H
#define WINDOW_H
#include <SDL2/SDL.h>
#include "rect.h"
/**
* Class to create window to render on
**/
class Window
{
public:
Window();
~Window();
SDL_Surface *get_screen_surface();
void Update();
private:
SDL_Window *window;
SDL_Surface *screen_surface = NULL;
};
#endif
window.o 0 → 100644
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment