Skip to content

Commit

Permalink
fully working buttons, bigger window
Browse files Browse the repository at this point in the history
Changes:
-used "map" containers to store objects
-added a "delete object" button
-info overlay works with new objects
-highlighting no longer makes text crash
-buttons brighten when hovered over
-increased window size
-disabled shooting
-updated readme
  • Loading branch information
confuzedskull committed Oct 14, 2014
1 parent 2ffd3fc commit 0981349
Show file tree
Hide file tree
Showing 20 changed files with 240 additions and 174 deletions.
3 changes: 3 additions & 0 deletions include/button.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ class button : public clickable_object
float label_margin;//space between text and button border
float font_size;//the height of the font in pt (not pixels)
float spacing;//space between each line
bool hovered_over();
bool left_clicked();
bool performed_action;
void fit_label();//adjusts the size of the button to fit the text
void set_label(char* l);
void mouse_function();
Expand Down
1 change: 1 addition & 0 deletions include/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class color
float b;//blue value
float a;//alpha value
bool changed;//whether the color values have been modified
void randomize();//generate a random color
void brighten();//increase the brightness of the color
void brighten(float brightness);//increase brightness by given value
void darken();//decrease brightness of the color
Expand Down
1 change: 1 addition & 0 deletions include/cursor.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,6 @@ class cursor
static int objects_selected();
static void calc_boundaries();
static void selection_box();
static void delete_selected();
};
#endif // CURSOR_H
1 change: 1 addition & 0 deletions include/draggable_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class draggable_object: public clickable_object, public tangible_object
{
public:
static void add_to_game();
static point2f origin;
bool grabbed();//checks if this object was grabbed by the cursor
void mouse_function();//performs a variety of actions dependent on cursor interaction
void update();
Expand Down
14 changes: 9 additions & 5 deletions include/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "button.h"
#include <time.h>
#include <vector>
#include <map>

//This class stores important settings and global variables for the program
class game
Expand All @@ -31,12 +32,15 @@ class game
static float time;//stores the current game time in seconds
static clock_t time_started;
static double time_elapsed;
//below I used pointers because the objects will be created in init_objects()
static std::vector<draggable_object*> draggable_objects;
static std::vector<physics_object*> physics_objects;
static std::vector<rts_object*> rts_objects;
//maps are used for these objects because cursor::left_clicked_object points to clickable_object exclusively
//object pointers are used because the objects are being created in init_objects() and we just need to reference them
static std::map<int,draggable_object*> draggable_objects;
static std::map<int,physics_object*> physics_objects;
static std::map<int,rts_object*> rts_objects;
//projectiles don't need to be individually initialized so pointers aren't used
//static std::vector<projectile> projectiles;//projectiles don't need to be initialized so they aren't pointers
//buttons are just like the other objects but they don't need to be found with a key
static std::vector<button*> buttons;
static std::vector<projectile> projectiles;//projectiles don't need to be initialized so they aren't pointers
static void init_objects();//initialize the objects
static void collision_detection();
};
Expand Down
1 change: 1 addition & 0 deletions include/physics_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
class physics_object: public clickable_object, public tangible_object
{
public:
static point2f origin;
static void add_to_game();
float mass;
float delta_time[6];/*each element of this array represents how much time a change in a direction or velocity took
Expand Down
1 change: 1 addition & 0 deletions include/rts_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class rts_object: public clickable_object, public tangible_object
{
public:
static void add_to_game();
static point2f origin;
bool highlighted();//checks if this object lies within the highlighting box
void mouse_function();//performs a variety of actions dependent on cursor interaction
void update();
Expand Down
25 changes: 13 additions & 12 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ A. Windows
1. If the program doesn't run, then you must right click it and select "Run as Administrator"

B. Mac
1. If the message ""2dworld" was blocked from opening because it is not from an identified developer" do the following:
1. If the message " "2dworld" was blocked from opening because it is not from an identified developer" do the following:
a. Go to System Preferences and click on Security & Privacy.
b. Somewhere near the bottom you'll see a message and then a button "Open Anyway". Click it.
c. A prompt will come up saying ""2dworld is a Unix applicaiton downloaded from the internet. Are you sure you want to open it?" Click open.
Expand All @@ -26,28 +26,29 @@ b. right click empty space to set the rally point and make the selected object m
c. right click an object to make the selected object follow it
d. right drag to have the selected object follow the cursor
3. Draggable Objects
-left click and drag an object to move it around
a. left click and drag an object to move it around

B. Keyboard
1. Global
a. i for information overlay
a. 'i' for information overlay
b. esc to quit
2. Physics Objects
a. W,A,S,D to move up,left,down,right
b. Q and E to rotate
a. 'W','A','S','D' to move up,left,down,right, respectively
b. 'Q' and 'E' to rotate
c. spacebar to shoot


III. Known Bugs:
A. Global
1. The information overlay sometimes crashes.
B. Physics Objects
1. Sometimes objects stop shooting for no reason.
A. Physics Objects
1. Shooting no longer works.
2. An object's resting point gets set even when it's moving.
3. Some of the object's physics values do not zero out when object is at rest.
C. RTS Objects
B. RTS Objects
1. An object's resting point doesn't get set at all.

Disclaimer:
This program was made using the OpenGL Utility Toolkit(GLUT) created by Mark Kilgard.
I do not own the rights to this API nor do I claim it as my own.
This program uses the OpenGL Utility Toolkit(A.K.A."GLUT") created by Mark Kilgard.
Thanks to his work, creating cross-platform OpenGL applications is much easier.
Since glut is not open source, I have not touched any of the source files.
Instead, I have linked the library to the executable and included glut32.dll in the project directory.
For more information on GLUT, visit https://www.opengl.org/resources/libraries/glut/
35 changes: 29 additions & 6 deletions src/button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "button.h"
#include "ui.h"
#include "window.h"
#include "cursor.h"
#ifdef __APPLE__
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
Expand All @@ -30,6 +31,27 @@
#include <GL/glut.h>
#endif
#include <iostream>
#include <math.h>

bool button::hovered_over()
{
if(!cursor::left_click &&
isless(cursor::passive.x,xmax) && isgreater(cursor::passive.x,xmin) &&
isless(cursor::passive.y,ymax) && isgreater(cursor::passive.y,ymin))
return true;
else
return false;
}

bool button::left_clicked()
{
if(cursor::left_click &&
isless(cursor::left_down.x,xmax) && isgreater(cursor::left_down.x,xmin) &&
isless(cursor::left_down.y,ymax) && isgreater(cursor::left_down.y,ymin))
return true;
else
return false;
}

void button::fit_label()
{
Expand All @@ -46,18 +68,18 @@ void button::set_label(char* l)

void button::mouse_function()
{
/*if(hovered_over() && !primary_color.changed)
if(hovered_over() && !primary_color.changed)
primary_color.brighten();
if(!hovered_over())
primary_color.undo();*/

if(left_clicked() && !primary_color.changed)//clicked this object
primary_color.undo();
if(left_clicked() && !performed_action)
{
primary_color.darken();
action();
performed_action=true;
}
if(!left_clicked())
primary_color.undo();
performed_action=false;

}

void button::render()
Expand Down Expand Up @@ -90,5 +112,6 @@ button::button()
fit_label();
primary_color.set(0.75f,0.75f,0.75f);
primary_color.changed=false;
performed_action=false;
std::clog<<"object#"<<number<<": "<<name<<'('<<type<<')'<<" created. "<<sizeof(*this)<<" bytes"<<std::endl;
}
12 changes: 12 additions & 0 deletions src/color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@

#include "color.h"
#include <math.h>
#include <stdlib.h>
#include <time.h>

void color::randomize()
{
srand(time(NULL));//initialize a new random seed
r=(rand()%10+1)/10.0f;//generate a number between 1 and 10, then divide by 10(so that it is in range 0.1 - 1.0)
//srand(time(NULL));//initialize a new random seed
g=(rand()%10+1)/10.0f;//generate a number between 1 and 10, then divide by 10(so that it is in range 0.1 - 1.0)
//srand(time(NULL));//initialize a new random seed
b=(rand()%10+1)/10.0f;//generate a number between 1 and 10, then divide by 10(so that it is in range 0.1 - 1.0)
}

void color::brighten()
{
Expand Down
42 changes: 21 additions & 21 deletions src/controls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,27 @@ bool controls::toggle_overlay = false;
void controls::check_clicked()
{
//The only way to check if no objects are being clicked is by checking every object
bool left_clicked = true;
for(unsigned a=0; a<game::rts_objects.size(); a++)
bool left_clicked=true;
bool right_clicked=true;
for(std::map<int,rts_object*>::iterator a=game::rts_objects.begin(); a!=game::rts_objects.end(); ++a)
{
if(left_clicked && game::rts_objects[a]->left_clicked())
if(left_clicked && a->second->left_clicked())
left_clicked=true;
else
left_clicked=false;
}
cursor::left_clicked_an_object = left_clicked;
bool right_clicked=true;
for(unsigned a=0; a<game::rts_objects.size(); a++)
{
if(right_clicked && game::rts_objects[a]->right_clicked())

if(right_clicked && a->second->right_clicked())
right_clicked=true;
else
right_clicked=false;
}
cursor::left_clicked_an_object = left_clicked;
cursor::right_clicked_an_object = right_clicked;

bool grabbed=true;
for(unsigned a=0; a<game::draggable_objects.size(); a++)
for(std::map<int,draggable_object*>::iterator a=game::draggable_objects.begin(); a!=game::draggable_objects.end(); ++a)
{
if(grabbed && game::draggable_objects[a]->grabbed())
if(grabbed && a->second->grabbed())
grabbed=true;
else
grabbed=false;
Expand Down Expand Up @@ -158,31 +157,32 @@ void controls::key_released(unsigned char key, int x, int y)

void controls::key_operations(void)
{
unsigned index = cursor::selected_object;//number of the currently selected object
if(strcmp(cursor::left_clicked_object->get_type(), "physics object")==0)
{
if(key_states['w'] || key_states['W'])
game::physics_objects[cursor::selected_object]->move_forward();
game::physics_objects[index]->move_forward();

if(key_states['s'] || key_states['S'])
game::physics_objects[cursor::selected_object]->move_back();
game::physics_objects[index]->move_back();

if(key_states['a'] || key_states['A'])
game::physics_objects[cursor::selected_object]->move_left();
game::physics_objects[index]->move_left();

if(key_states['d'] || key_states['D'])
game::physics_objects[cursor::selected_object]->move_right();
game::physics_objects[index]->move_right();

if(key_states['q'] || key_states['Q'])
game::physics_objects[cursor::selected_object]->turn_left();
game::physics_objects[index]->turn_left();

if(key_states['e'] || key_states['E'])
game::physics_objects[cursor::selected_object]->turn_right();
game::physics_objects[index]->turn_right();

if(key_states[32])//spacebar
/*if(key_states[32])//spacebar
{
if(!game::projectiles[cursor::selected_object].fired)
game::projectiles[cursor::selected_object].fire(*game::physics_objects[cursor::selected_object]);
}
if(!game::projectiles[index].fired)
game::projectiles[index].fire(*game::physics_objects[index]);
}*/
}
if(key_states['i'] || key_states['I'])
{
Expand Down
15 changes: 14 additions & 1 deletion src/cursor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include <GL/glu.h>
#include <GL/glut.h>
#endif

#include <iostream>
//initialize static variables
point2f cursor::passive = point2f(0.0,0.0);
bool cursor::left_click = false;
Expand Down Expand Up @@ -77,6 +77,9 @@ void cursor::selection_box()//this is the box that is created when user clicks a
{
if(highlighting)
{
bool blending = false;
if(glIsEnabled(GL_BLEND))
blending = true;
glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glColor4f(box_color.r,box_color.g,box_color.b,0.5);
Expand All @@ -86,5 +89,15 @@ void cursor::selection_box()//this is the box that is created when user clicks a
glVertex2f(xmax, ymax); // The top right corner
glVertex2f(xmax, ymin); // The bottom right corner
glEnd();
if(!blending)
glDisable(GL_BLEND);
}
}

void cursor::delete_selected()
{
game::draggable_objects.erase(selected_object);
game::physics_objects.erase(selected_object);
game::rts_objects.erase(selected_object);
std::clog<<"object#"<<left_clicked_object->get_number()<<": "<<left_clicked_object->name<<'('<<left_clicked_object->get_type()<<')'<<" deleted."<<std::endl;
}
7 changes: 6 additions & 1 deletion src/draggable_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "complex_object.h"
#include "cursor.h"
#include "distance.h"
#include "window.h"
#include <math.h>
#include <iostream>
#ifdef __APPLE__
Expand All @@ -33,9 +34,11 @@
#include <GL/glut.h>
#endif

point2f draggable_object::origin = point2f(window::width*0.9,window::height*0.5);

void draggable_object::add_to_game()
{
game::draggable_objects.push_back(new draggable_object());
game::draggable_objects.insert(std::pair<int,draggable_object*>(object::total_objects,new draggable_object()));//add object to container
}

bool draggable_object::grabbed()
Expand Down Expand Up @@ -93,5 +96,7 @@ void draggable_object::update()
draggable_object::draggable_object()
{
type="draggable object";
primary_color=BLACK;
position.set(origin);
std::clog<<"object#"<<number<<": "<<name<<'('<<type<<')'<<" created. "<<sizeof(*this)<<" bytes"<<std::endl;
}
Loading

0 comments on commit 0981349

Please sign in to comment.