Skip to content

Commit

Permalink
Work
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-andreas committed Jun 23, 2013
1 parent 2226161 commit 540d469
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 5 deletions.
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ FILE(GLOB MyCSources *.cpp)

ADD_EXECUTABLE(neuralnet ${MyCSources})

include_directories(neuralnet /usr/include/SDL)

install(TARGETS neuralnet RUNTIME DESTINATION bin)

target_link_libraries(neuralnet SDL SDL_image)
target_link_libraries(neuralnet SDL SDL_image SDL_gfx)
42 changes: 42 additions & 0 deletions gamestate.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "gamestate.h"

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

Sweeper::Sweeper() {
int posx = 0;
Expand All @@ -11,3 +13,43 @@ Gamestate::Gamestate(int boardWidth, int boardHeight) {
this->boardWidth = boardWidth;
this->boardHeight = boardHeight;
}

void moveSweeper(Gamestate *gs, Sweeper &sweeper) {
double lTrack, rTrack;
lTrack = rand() / ((double)RAND_MAX/2) - 1.0;
rTrack = rand() / ((double)RAND_MAX/2) - 1.0;

lTrack = 1.0;
rTrack = 0.95;

double rotForce = lTrack - rTrack;
if(rotForce < -0.3) rotForce = -0.3;
if(rotForce > 0.3) rotForce = 0.3;

sweeper.rotation += rotForce;

double speed = lTrack + rTrack;

//update Look At
double vLookAtX = cos(sweeper.rotation);
double vLookAtY = -sin(sweeper.rotation);

//update position
sweeper.posx += vLookAtX * speed;
sweeper.posy += vLookAtY * speed;

// printf(
// "Rotation: %f speed %f pos %d,%d lookat %f,%f degress %f\n", sweeper.rotation, speed,
// (int)sweeper.posx, (int)sweeper.posy, vLookAtX, vLookAtY, sweeper.rotation*180.0/M_PI
// );
}

void moveSweepers(Gamestate *gs) {
for(std::vector<Sweeper>::iterator i = gs->sweepers.begin(); i != gs->sweepers.end(); i++) {
moveSweeper(gs, *i);
}
}

void doTurn(Gamestate* gs) {
moveSweepers(gs);
}
7 changes: 6 additions & 1 deletion gamestate.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ class Sweeper {
public:
Sweeper();

int posx, posy;
double posx, posy;

// Rotation, value between 0 and 1
double rotation;
};

class Gamestate {
Expand All @@ -19,4 +22,6 @@ class Gamestate {
std::vector<Sweeper> sweepers;
};

void doTurn(Gamestate *gs);

#endif
20 changes: 19 additions & 1 deletion gfx.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
#include "gfx.h"

#include <SDL/SDL_rotozoom.h>
#include <SDL/SDL_timer.h>
#include <math.h>

const int FRAMES_PER_SECOND = 60;
const int FRAMERATE_IN_MS = 1000/FRAMES_PER_SECOND;
const char* WINDOW_TITLE = "SDL Start";

void plotSweeper(sdlgamestate_t *g, const Sweeper &sweeper) {
Expand All @@ -17,7 +23,10 @@ void plotSweeper(sdlgamestate_t *g, const Sweeper &sweeper) {
destination.w = 65;
destination.h = 44;

SDL_BlitSurface(g->bitmaps.sweeper, &source, g->screen, &destination);
SDL_Surface *rotated = rotozoomSurface(g->bitmaps.sweeper, sweeper.rotation*180.0/M_PI-90.0, 1, 1);
SDL_BlitSurface(rotated, &source, g->screen, &destination);

SDL_FreeSurface(rotated);
}

void plotSweepers(sdlgamestate_t *g) {
Expand Down Expand Up @@ -48,17 +57,26 @@ int sdlMainLoop(sdlgamestate_t *g) {

SDL_Event event;
bool gameRunning = true;
unsigned int startTime, endTime;

while (gameRunning) {
startTime = SDL_GetTicks();

if (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
gameRunning = false;
}
}

SDL_FillRect(g->screen, NULL, SDL_MapRGB(g->screen->format, 255, 255, 255));
doTurn(g->gamestate);
plotSweepers(g);

SDL_Flip(g->screen);

endTime = SDL_GetTicks();

SDL_Delay(FRAMERATE_IN_MS - (endTime - startTime));
}

SDL_FreeSurface(g->bitmaps.sweeper);
Expand Down
2 changes: 0 additions & 2 deletions gfx.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ typedef struct {
struct {
SDL_Surface *sweeper;
} bitmaps;


} sdlgamestate_t;

int sdlMainLoop(sdlgamestate_t *g);
Expand Down
2 changes: 2 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ int main(int argc, char **argv) {
g.gamestate = &gs;

gs.sweepers.push_back(Sweeper());
gs.sweepers[0].posx = 320;
gs.sweepers[0].posy = 240;

return sdlMainLoop(&g);
}

0 comments on commit 540d469

Please sign in to comment.