Skip to content

Instantly share code, notes, and snippets.

@protectivetoast83
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save protectivetoast83/25301772cbc6f13e6e07 to your computer and use it in GitHub Desktop.
Save protectivetoast83/25301772cbc6f13e6e07 to your computer and use it in GitHub Desktop.
Early engine
#include "gameWorld.h"
#include <iostream>
using namespace std;
extern void time();
extern void controls();
gameWorld::gameWorld(SDL_Surface * newScreen)
{
screen = newScreen;
}
void gameWorld::create(int type, int x, int y)
{
for(int i=0; i<MAX_OBJECTS; i++)
{
if(!objSet[i].checkActive())
{
objSet[i].instansiate(type, x, y);
// cout<<"object: "<<i<<endl;
i=MAX_OBJECTS;
}
}
}
void gameWorld::runObjects()
{
// time();
// controls();
for(int i=0; i<MAX_OBJECTS; i++)
{
if(objSet[i].checkActive())
{
objSet[i].update();
for(int j=0; j<MAX_OBJECTS; j++)
{
if( objSet[i].checkCollision(objSet[j]) )
{
objSet[i].collision(objSet[j]);
objSet[j].collision(objSet[i]);
}
}
}
}
for(int i=0; i<MAX_OBJECTS; i++)
{
if(objSet[i].checkActive())
{
render.drawSprite(objSet[i].img, objSet[i].destination(), screen);
cout<<"object: "<<i<<endl;
}
}
}
#include "gameWorld.h"
#include <iostream>
using namespace std;
//extern void time();
//extern void controls();
gameWorld::gameWorld(SDL_Surface * newScreen)
{
screen = newScreen;
}
void gameWorld::create(int type, int x, int y)
{
for(int i=0; i<MAX_OBJECTS; i++)
{
if(!objSet[i].checkActive())
{
objSet[i].instansiate(type, x, y);
// cout<<"object: "<<i<<endl;
i=MAX_OBJECTS;
}
}
}
void gameWorld::runObjects()
{
// time();
// controls();
for(int i=0; i<MAX_OBJECTS; i++)
{
if(objSet[i].checkActive())
{
objSet[i].update();
for(int j=0; j<MAX_OBJECTS; j++)
{
if( objSet[i].checkCollision(objSet[j]) )
{
objSet[i].collision(objSet[j]);
objSet[j].collision(objSet[i]);
}
}
}
}
for(int i=0; i<MAX_OBJECTS; i++)
{
if(objSet[i].checkActive())
{
render.drawSprite(objSet[i].img, objSet[i].destination(), screen);
// cout<<"object: "<<i<<endl;
}
}
}
#ifndef _KEYS_H
#define _KEYS_H
#include "SDL.h"
bool WKEY, AKEY, SKEY, DKEY, SPACEBAR, ESC;
void runKeys(SDL_Event event)
{
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_KEYDOWN:
{
switch(event.key.keysym.sym)
{
case SDLK_w: WKEY=true;
break;
case SDLK_a: AKEY=true;
break;
case SDLK_d: DKEY=true;
break;
case SDLK_s: SKEY=true;
}
}
case SDL_KEYUP:
{
switch(event.key.keysym.sym)
{
case SDLK_w: WKEY=false;
break;
case SDLK_a: AKEY=false;
break;
case SDLK_d: DKEY=false;
break;
case SDLK_s: SKEY=false;
}
}
}
}
}
#endif
#include "SDL.h"
#include "gameWorld.h"
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
SDL_Window* window;
SDL_Surface* surface = NULL;
SDL_Surface* screen = NULL;
surface = SDL_LoadBMP( "helloworld.bmp" );
// Initialize SDL.
if (SDL_Init(SDL_INIT_VIDEO) < 0)
return 1;
// Create the window where we will draw.
window = SDL_CreateWindow("SDL_RenderClear",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
512, 512,
0);
screen = SDL_GetWindowSurface( window );
gameWorld game(screen); ///
// game.create(1, 0, 0);
game.create(1, 32, 32);
// xmlMap();
bool running=true;
int i=0;
while(running){
// runTime();
// everythingLogic(screen);
// SDL_BlitSurface( surface, NULL, screen, NULL );
// SDL_Delay(2000);
SDL_UpdateWindowSurface( window );
SDL_FillRect(screen, NULL, 0x000000);
game.runObjects();
}
// SDL_Delay(3000);
SDL_Quit();
return 0;
}
#include "object.h"
//#include "time.h"
bool object::checkCollision(object obj){
if( ((x>obj.x && x<obj.x+obj.w) || (obj.x>x && obj.x < x+w)) && ((y>obj.y && y<obj.y+obj.h) || (obj.y>y && obj.y < y+h)) )
return true;
else
return false;
}
void object::normalForce(object obj)
{
if(solid==true)
while( checkCollision(obj) )
{
if(x<x2)
x2--;
else
if(x>x2)
x++;
if(y<y2)
y2--;
else
if(y>y2)
y++;
}
}
void object::movePotential()
{
x2=x+vx*deltaTime;
y2=y+vy*deltaTime;
}
void object::moveFinal()
{
x=x2;
y=y2;
}
void object::gravity()
{
if(vy<10)
vy++;
}
#ifndef _OBJECT_H
#define _OBJECT_H
#include "SDL.h"
#include <iostream>
#include "time.h"
using namespace std;
class object
{
//Basic variables
int x=0, y=0, z=0, h=0, w=0, d=0;
int x2=0, y2=0, z2=0;
int vx=0, vy=0;
bool active=false;
int type=0;
//specific variables
//private functions
void gravity();
void normalForce(object);
void movePotential();
void moveFinal();
public:
object();
void instansiate(int, int, int);
//public variables
bool solid=false;
int img=0;
//public functions
bool checkCollision(object);
SDL_Rect destination();
bool checkActive();
//main functions
void update();
void collision(object);
};
//bool object::checkCollision(object obj);
#endif
#include "object.h"
SDL_Rect object::destination()
{
SDL_Rect dst;
dst.x = x;
dst.y=y;
return dst;
}
#include "keys.h"
#include "object.h"
#include <iostream>
using namespace std;
object::object()
{
active=false;
}
void object::instansiate(int t, int newx, int newy)
{
type=t;
active=true;
x=newx;
y=newy;
switch(t)
{
case 1:
{
img = 1;
}
break;
case 2:
{
img = 2;
}
}
}
void object::update()
{
switch(type)
{
case 1:
{
movePotential();
moveFinal();
gravity();
}
}
}
void object::collision(object obj)
{
switch(type)
{
case 1:
{
if(AKEY==true)
vx=-1;
if(DKEY==true)
vx=1;
}
case 2:
{
obj.normalForce(*this); ///hopefully this works
}
}
}
bool object::checkActive()
{
return active;
}
#include "renderer.h"
renderer::renderer()
{
player = SDL_LoadBMP("ninja.bmp");
ground = SDL_LoadBMP("ground.bmp");
}
void renderer::drawSprite(int image, SDL_Rect dst, SDL_Surface * screen)
{
SDL_Surface * sprite;
switch(image)
{
case 1: sprite = player;
break;
case 2: sprite = ground;
break;
default:
break;
}
;
SDL_BlitSurface(sprite, NULL, screen, &dst);
}
#ifndef _RENDERER_H
#define _RENDERER_H
#include "SDL.h"
class renderer
{
// SDL_Surface * screen;
SDL_Surface * player;
SDL_Surface * ground;
public:
void drawSprite(int, SDL_Rect, SDL_Surface * screen);
renderer();
};
#endif
#ifndef _TIME_H
#define _TIME_H
int deltaTime=0;
double totalTime=0;
void runTiime()
{
deltaTime=SDL_GetTicks() - totalTime;
totalTime=SDL_GetTicks();
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment