Updated structure, added event loop
parent
cc8ea02590
commit
200e2c591d
2
makefile
2
makefile
|
@ -1,5 +1,5 @@
|
|||
#OBJS specifies which files to compile as part of the project
|
||||
OBJS = src/main.cpp src/main_window.cpp
|
||||
OBJS = src/main.cpp src/main_window.cpp src/random_gen.cpp
|
||||
|
||||
#CC specifies which compiler we're using
|
||||
CC = g++
|
||||
|
|
|
@ -1,6 +0,0 @@
|
|||
#ifndef CONTROLS_H
|
||||
#define CONTROLS_H
|
||||
|
||||
|
||||
|
||||
#endif
|
|
@ -3,12 +3,13 @@
|
|||
#include "main_window.h"
|
||||
#include "random_gen.h"
|
||||
|
||||
int main( int argc, char* args[] )
|
||||
int main( int argc, char* argv[] )
|
||||
{
|
||||
//ran::args the_args = ran::parse(argc, args)
|
||||
ran::args the_args;
|
||||
ran::args the_args = ran::parse(argc, argv);
|
||||
if(the_args.num_clusters == -1) return 1;
|
||||
|
||||
if (mainw::initialize(&the_args)) return 1;
|
||||
|
||||
if (mainw::initialize(&the_args)) return 1;
|
||||
|
||||
mainw::loop();
|
||||
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
#include "main_window.h"
|
||||
#include "controls.h"
|
||||
#include <SDL.h>
|
||||
#include <iostream>
|
||||
|
||||
|
@ -13,8 +12,8 @@ bool run = true;
|
|||
int mainw::initialize(ran::args* arg)
|
||||
{
|
||||
window = NULL;
|
||||
screenSurface = NULL;()
|
||||
if( SDL_Init( SDL_INIT_VIDEO ) < 0 )()
|
||||
screenSurface = NULL;
|
||||
if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
|
||||
{
|
||||
printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
|
||||
return 1;
|
||||
|
@ -32,20 +31,33 @@ int mainw::initialize(ran::args* arg)
|
|||
|
||||
void mainw::loop()
|
||||
{
|
||||
con::control_struct controls;
|
||||
while(run)
|
||||
{
|
||||
con::do_controls(&control_struct);
|
||||
|
||||
//If quit is pressed
|
||||
if(control_struct.quit)
|
||||
break; //out of while loop
|
||||
|
||||
//respond to controls
|
||||
respond(&control_struct); //sets player position, toggles check
|
||||
|
||||
if(timeHasPassed)
|
||||
tick(); //renders screen, runs the toggled check
|
||||
bool quit = false;
|
||||
SDL_Event event;
|
||||
while(!quit)
|
||||
{ //Event Loop
|
||||
while( SDL_PollEvent( &event ) )
|
||||
{
|
||||
if( event.type == SDL_QUIT )
|
||||
quit = true;
|
||||
if( event.type == SDL_KEYDOWN ) {
|
||||
switch( event.key.keysym.sym )
|
||||
{
|
||||
case SDLK_UP: up(true); break;
|
||||
case SDLK_DOWN: down(true); break;
|
||||
case SDLK_LEFT: left(true); break;
|
||||
case SDLK_RIGHT: right(true); break;
|
||||
}
|
||||
}
|
||||
if( event.type == SDL_KEYUP ) {
|
||||
switch( event.key.keysym.sym )
|
||||
{
|
||||
case SDLK_UP: up(false); break;
|
||||
case SDLK_DOWN: down(false); break;
|
||||
case SDLK_LEFT: left(false); break;
|
||||
case SDLK_RIGHT: right(false); break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -57,3 +69,37 @@ void mainw::quit()
|
|||
//Quit SDL subsystems
|
||||
SDL_Quit();
|
||||
}
|
||||
|
||||
void mainw::tick(){}
|
||||
|
||||
void mainw::up(bool pressed)
|
||||
{
|
||||
if(pressed)
|
||||
std::cout << "UP." << std::endl;
|
||||
else
|
||||
std::cout << "UP^" << std::endl;
|
||||
}
|
||||
|
||||
void mainw::down(bool pressed)
|
||||
{
|
||||
if(pressed)
|
||||
std::cout << "DOWN." << std::endl;
|
||||
else
|
||||
std::cout << "DOWN^" << std::endl;
|
||||
}
|
||||
|
||||
void mainw::left(bool pressed)
|
||||
{
|
||||
if(pressed)
|
||||
std::cout << "LEFT." << std::endl;
|
||||
else
|
||||
std::cout << "LEFT^" << std::endl;
|
||||
}
|
||||
|
||||
void mainw::right(bool pressed)
|
||||
{
|
||||
if(pressed)
|
||||
std::cout << "RIGHT." << std::endl;
|
||||
else
|
||||
std::cout << "RIGHT^" << std::endl;
|
||||
}
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
#define MAIN_WINDOW_H
|
||||
|
||||
#include <SDL.h>
|
||||
#include "controls.h"
|
||||
#include "random_gen.h"
|
||||
|
||||
namespace mainw {
|
||||
|
@ -30,12 +29,13 @@ void loop();
|
|||
//
|
||||
void quit();
|
||||
|
||||
//-------------------------
|
||||
// mainw::respond()
|
||||
// Uses controls from keyboard to
|
||||
// move the player, change the current check, etc.
|
||||
//
|
||||
void respond(con::control_struct controls);
|
||||
|
||||
void tick();
|
||||
|
||||
void up(bool pressed);
|
||||
void down(bool pressed);
|
||||
void left(bool pressed);
|
||||
void right(bool pressed);
|
||||
|
||||
} //End mainw namespace
|
||||
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
#include <iostream>
|
||||
#include <string>
|
||||
#include "random_gen.h"
|
||||
|
||||
ran::args ran::parse(int argc, char* argv[])
|
||||
{
|
||||
args local;
|
||||
local.num_clusters = -1;
|
||||
//check proper number of arguments
|
||||
if(argc ==4) {
|
||||
local.num_clusters = std::stoi(argv[1]);
|
||||
local.num_total_points = std::stoi(argv[2]);
|
||||
local.spread_multiplier = std::stoi(argv[3]);
|
||||
} else {
|
||||
//show usage
|
||||
std::cout << "Improper usage entered, please enter 3 integers:" << std::endl;
|
||||
std::cout << "num_clusters, num_total_points, spread_multiplier" << std::endl;
|
||||
}
|
||||
return local;
|
||||
}
|
|
@ -2,15 +2,22 @@
|
|||
#define RANDOM_GEN_H
|
||||
|
||||
namespace ran {
|
||||
|
||||
|
||||
struct args {
|
||||
int num_clusters;
|
||||
int num_total_points;
|
||||
float spread_multiplier;
|
||||
};
|
||||
|
||||
//---------------------
|
||||
// ran::parse(int arc, char* argv);
|
||||
//
|
||||
// Only 4 paramaters are allowed, no more or less.
|
||||
// Must provide num_clusters num_total_points spread_multiplier
|
||||
// Otherwise, usage is printed to stdout
|
||||
args parse(int argc, char* argv[]);
|
||||
|
||||
|
||||
void generate(args the_args);
|
||||
|
||||
}
|
||||
#endif
|
||||
|
|
Loading…
Reference in New Issue