1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
#include <Windows.h>
#include <iostream>
//box2D
#include <Box2D\Box2D.h>
//allegro
#include <allegro5\allegro.h>
#include <allegro5\allegro_native_dialog.h>
#include <allegro5\allegro_image.h>
#include <allegro5\allegro_font.h>
#include <allegro5\allegro_ttf.h>
#include <allegro5\allegro_primitives.h>
using namespace std;
#define ScreenHeight 600
#define ScreenWidth 800
#define Pix 32
int main(){
/*B2_NOT_USED(argc);
B2_NOT_USED(argv);*/
ALLEGRO_DISPLAY *display;//create a display
//ALLEGRO_DISPLAY_MODE disp_data;
const float FPS = 40.0;
if(!al_init()){
al_show_native_message_box(NULL, "Allegro Error","ERROR:","Could not initialize Allegro 5", NULL, NULL);
return -1;
}
//continue, no error to initialize allegro
//al_get_display_mode(al_get_num_display_modes() - 1, &disp_data);
al_set_new_display_flags(ALLEGRO_WINDOWED);//set window to fullScreen
display = al_create_display(ScreenWidth, ScreenHeight);//create window
al_set_window_title(display, "Allegro Physics");//change window title
if(!display){
al_show_native_message_box(display, "Allegro Error","ERROR:","Could not create window", NULL, NULL);
return -1;
}
al_install_keyboard();
al_init_image_addon();
al_init_primitives_addon();
al_init_font_addon();//initialize fonts
al_init_ttf_addon();//initialize
ALLEGRO_FONT *font = al_load_font("maiden.ttf", 16, NULL);
if(!font){
al_show_native_message_box(display, "Allegro Error","ERROR:","Could not initialize font addon", NULL, NULL);
return -1;
}
ALLEGRO_COLOR green = al_map_rgb(0,255,0);
ALLEGRO_COLOR black = al_map_rgb(0,0,0);
//initialize events system
ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue();//create a event hanndler
ALLEGRO_TIMER *timer = al_create_timer(1.0/FPS);//60 fps
al_register_event_source(event_queue, al_get_timer_event_source(timer));//register a event to update timer
al_register_event_source(event_queue, al_get_keyboard_event_source());//register a event to update mouse
al_register_event_source(event_queue, al_get_display_event_source(display));//register a event updater to update display(game window)
//end
//vars
bool hasDone = false, draw = true, jumping = false;
float PosX=0,PosY=ScreenHeight-1,radius = 30, speed = 2;
//end
//box2D
//world
b2Vec2 gravity(0.0f, 10.0f);//gravity
b2World world(gravity);//create world
b2BodyDef groundBodyDef;//create a definition of the ground(floor)
//world
//ground
//PS:the position of box2D is on center!
groundBodyDef.position.Set(ScreenWidth/Pix, ScreenHeight/Pix);//define the position of ground
b2Body* groundBody = world.CreateBody(&groundBodyDef);//create the body of ground
b2PolygonShape groundBox;//create the shape of ground
float groundWidth = 6.0f;//ground width
float groundHeight = 2.4f;//ground height
groundBox.SetAsBox(groundWidth/2, groundHeight/2);
groundBody->CreateFixture(&groundBox, 0.0f);
//ground
//player
b2BodyDef playerDef;
playerDef.type = b2_dynamicBody;
playerDef.position.Set(PosX/Pix,PosY/Pix); //top of the screen
b2Body* playerBody = world.CreateBody(&playerDef);
b2PolygonShape dynamicBox;
float playerWidth = radius/Pix;
float playerHeight = radius/Pix;
dynamicBox.SetAsBox(playerWidth/2, playerHeight/2);
b2FixtureDef fixtureDef;
fixtureDef.shape = &dynamicBox;
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.3f;
playerBody->CreateFixture(&fixtureDef);
//player
//misc
float timeStep = 1/FPS;
int32 velocityIterations = 10;
int32 positionIterations = 10;
//misc
//end
al_start_timer(timer);//start timer counter
//remeber dont put anithing here, you only put the game loop
while(!hasDone){
ALLEGRO_EVENT events;
ALLEGRO_KEYBOARD_STATE KeyState;
al_get_keyboard_state(&KeyState);
al_wait_for_event(event_queue, &events);
if(events.type == ALLEGRO_EVENT_TIMER){
world.Step(FPS, velocityIterations, positionIterations);
world.ClearForces();
if(al_key_down(&KeyState, ALLEGRO_KEY_UP)){
if(!jumping){
b2Vec2 force (0, -10/Pix);
playerBody->ApplyLinearImpulse(force, playerBody->GetWorldCenter());
jumping = true;
}
}if(al_key_down(&KeyState, ALLEGRO_KEY_LEFT)){
b2Vec2 force (-speed/Pix, 0);
playerBody->ApplyLinearImpulse(force, playerBody->GetWorldCenter());
}if(al_key_down(&KeyState, ALLEGRO_KEY_RIGHT)){
b2Vec2 force (speed/Pix, 0);
playerBody->ApplyLinearImpulse(force, playerBody->GetWorldCenter());
}if(al_key_down(&KeyState, ALLEGRO_KEY_ESCAPE)){
hasDone = true;
}
draw = true;
}else{
draw = false;
if(events.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
hasDone = true;
}
//-----//-----//
if(playerBody->GetPosition().y*radius <= groundBody->GetPosition().y*groundHeight){
//colliding with the ground
jumping = false;
}
//-----//-----//
if(draw){
b2Vec2 playerPosition = playerBody->GetPosition();
b2Vec2 groundPosition = groundBody->GetPosition();
al_draw_filled_rectangle(playerPosition.x*Pix-(radius/2), playerPosition.y*Pix-(radius/2), playerPosition.x*Pix+(radius/2), playerPosition.y*Pix+(radius/2), green);
al_draw_filled_rectangle(groundPosition.x*Pix-(groundWidth/2), groundPosition.y*Pix-(groundHeight/2), groundPosition.x*Pix+(groundWidth/2), groundPosition.y*Pix+(groundHeight/2), al_map_rgb(255,0,0));
al_flip_display();//draw the buffer
al_clear_to_color(black);//clear screen
}
}
//release
al_destroy_display(display);
al_destroy_font(font);
al_destroy_timer(timer);
return 0;
}
|