Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Tic

This thread is locked; no one can reply to it. rss feed Print
Tic
Antone333
Member #15,545
March 2014

Title was supposed to be tic tac toe. haha

Ok. i dont know what i am doing wrong. It takes a couple of clicks sometimes to get the X or O to go onto the board. please help. what am i doing wrong?

The next feature i am going to add is a button on the bottom to reset the game. but i know how to do that.

jmasterx
Member #11,410
October 2009

I think it is because you are constantly trying to render which is crippling the event queue.

Set up a timer and only render when there are no events in the queue and you get a tick
Here is my scene manager which does this sort of thing:

#SelectExpand
1 SceneManager::SceneManager( DeviceManager* settings, DynamicUIManager* dynamicUImanager ) 2 : m_devices(settings),m_gameIsRunning(true), m_nextScene(LOGIN), 3 m_currentScene(NULL),m_needsRedraw(true),m_frameRate(60.0f), 4 m_dynamicUI(dynamicUImanager), m_currentSceneType(NO_SCENE), 5 m_needsResize(false),m_newScreenHeight(0),m_newScreenWidth(0), 6 m_guiImageManager("res/gui/gui_image_mapping.conf","res/gui/") 7 { 8 queue = al_create_event_queue(); 9 m_gameTimer = al_create_timer(1.0 / m_frameRate); 10 registerEventSources(); 11 //Instance the first scene 12 processMessages(); 13 Log::write("Scene Manager","Scene Manager Started"); 14 m_g.setBuffering(false); 15 } 16 17 SceneManager::~SceneManager(void) 18 { 19 //delete the current scene 20 if(m_currentScene) 21 { 22 m_currentScene->sceneEnd(); 23 delete m_currentScene; 24 m_currentScene = NULL; 25 } 26 27 al_destroy_timer(m_gameTimer); 28 al_destroy_event_queue(queue); 29 Log::write("Scene Manager","Scene Manager Destroyed"); 30 31 } 32 33 void SceneManager::run() 34 { 35 m_devices->setSceneMessenger(this); 36 37 al_start_timer(m_gameTimer); 38 39 //is the event handled? 40 bool handled = false; 41 ALLEGRO_EVENT next; 42 int numTicks = 0; 43 44 //main loop 45 while(m_gameIsRunning) 46 { 47 48 handled = false; 49 al_wait_for_event(queue,&evt); 50 51 bool hasNext = al_peek_next_event(queue,&next); 52 if(hasNext && next.type == ALLEGRO_EVENT_TIMER) 53 { 54 al_drop_next_event(queue); 55 } 56 //render the scene 57 if(m_needsRedraw && al_is_event_queue_empty(queue)) 58 { 59 m_g.begin(); 60 m_currentScene->render(); 61 m_g.end(); 62 63 m_needsRedraw = false; 64 } 65 66 defaultBeginEventHandler(&evt); 67 m_currentScene->processEvent(&evt,handled); 68 69 //do default behavior if event was not handled by the scene 70 if (!handled) 71 { 72 defaultEndEventHandler(&evt); 73 } 74 75 processMessages(); 76 } 77 } 78 79 void SceneManager::registerEventSources() 80 { 81 al_register_event_source(queue, (ALLEGRO_EVENT_SOURCE*)al_get_keyboard_event_source()); 82 al_register_event_source(queue, (ALLEGRO_EVENT_SOURCE*)al_get_mouse_event_source()); 83 al_register_event_source(queue, (ALLEGRO_EVENT_SOURCE*)m_gameTimer); 84 al_register_event_source(queue, (ALLEGRO_EVENT_SOURCE*)m_devices->getDisplay()->getContext()); 85 Log::write("Scene Manager","Event sources registered"); 86 87 } 88 89 void SceneManager::sendSceneChangeMessage( SceneEnum scene ) 90 { 91 m_nextScene = scene; 92 } 93 94 void SceneManager::sendQuitMessage() 95 { 96 m_gameIsRunning = false; 97 } 98 99 void SceneManager::processMessages() 100 { 101 changeScene(); 102 } 103 104 void SceneManager::changeScene() 105 { 106 if(m_nextScene != NO_SCENE) 107 { 108 109 //delete the current scene 110 if(m_currentScene) 111 { 112 m_currentScene->sceneEnd(); 113 m_currentScene->deinitBaseScene(); 114 delete m_currentScene; 115 m_currentScene = NULL; 116 Log::write("Scene Manager","Scene Ended"); 117 } 118 119 m_currentSceneType = m_nextScene; 120 121 //instance the requested scene 122 switch(m_nextScene) 123 { 124 case SceneManagerMessage::GAME: 125 m_currentScene = new GameScene(); 126 Log::write("Scene Manager","Changed to Game Scene"); 127 break; 128 case SceneManagerMessage::LOBBY: 129 m_currentScene = new LobbyScene(); 130 Log::write("Scene Manager","Changed to Lobby Scene"); 131 break; 132 case SceneManagerMessage::LOGIN: 133 m_currentScene = new LoginScene(); 134 Log::write("Scene Manager","Changed to Login Scene"); 135 break; 136 } 137 138 m_nextScene = NO_SCENE; 139 140 //set some class pointers 141 m_currentScene->setGraphics(&m_g); 142 m_currentScene->setDeviceManager(m_devices); 143 m_currentScene->setDynamicUIManager(m_dynamicUI); 144 m_currentScene->setGuiImageManager(&m_guiImageManager); 145 m_currentScene->setSceneMessenger(this); 146 m_currentScene->setGameTimer(m_gameTimer); 147 m_currentScene->initBaseScene(); 148 m_currentScene->sceneBegin(); 149 m_currentScene->resizeEvent( 150 m_devices->getDisplay()->getResolution().getX(), 151 m_devices->getDisplay()->getResolution().getY()); 152 m_currentScene->initialResizeCompleted(); 153 m_currentScene->sceneLogic(); 154 m_currentScene->logic(); 155 156 157 } 158 } 159 160 void SceneManager::defaultEndEventHandler( ALLEGRO_EVENT* evt ) 161 { 162 switch(evt->type) 163 { 164 case ALLEGRO_EVENT_DISPLAY_CLOSE: 165 166 sendQuitMessage(); 167 break; 168 default: 169 break; 170 } 171 } 172 173 void SceneManager::defaultBeginEventHandler( ALLEGRO_EVENT*evt ) 174 { 175 176 m_currentScene->processGuiInputEvent(evt); 177 178 if(evt->type == ALLEGRO_EVENT_TIMER && evt->timer.source == m_gameTimer) 179 { 180 m_needsRedraw = true; 181 m_currentScene->sceneLogic(); 182 m_currentScene->processGuiLogic(); 183 m_currentScene->logic(); 184 m_devices->getNetClient()->tick(); 185 } 186 else if(evt->type == ALLEGRO_EVENT_DISPLAY_RESIZE) 187 { 188 m_needsResize = true; 189 m_newScreenWidth = evt->display.width; 190 m_newScreenHeight = evt->display.height; 191 sendResizeMessage(m_newScreenWidth,m_newScreenHeight); 192 } 193 else if(m_needsResize) 194 { 195 m_needsResize = false; 196 197 } 198 } 199 200 void SceneManager::sendResizeMessage( int w, int h ) 201 { 202 //this is a bug in Allegro 203 if(w == 0 && h == 0) 204 { 205 return; 206 } 207 208 al_acknowledge_resize(m_devices->getDisplay()->getContext()); 209 m_g.resizeBuffer(w,h); 210 m_currentScene->processGuiResizeEvent(); 211 m_currentScene->resizeEvent( 212 w,h); 213 Log::write("Scene Manager","Resize Event: Width:" + StringUtil::toString(w) + 214 " Height:" + StringUtil::toString(h)); 215 } 216 217 SceneManagerMessage::SceneEnum SceneManager::getCurrentScene() const 218 { 219 return m_currentSceneType; 220 } 221 222 bool SceneManager::willSceneChange() const 223 { 224 return m_nextScene != NO_SCENE; 225 } 226}

An easy fix that might work is:

#SelectExpand
1 if(al_is_event_queue_empty(EventQueue)) 2 { 3 if(one == 1) 4 { 5 al_draw_bitmap(X, 0, 0, 0); 6 } 7 else if(one == 2) 8 { 9 al_draw_bitmap(O, 0, 0, 0); 10 } 11 if(two == 1) 12 { 13 al_draw_bitmap(X, 32, 0, 0); 14 } 15 else if(two == 2) 16 { 17 al_draw_bitmap(O, 32, 0, 0); 18 } 19 if(three == 1) 20 { 21 al_draw_bitmap(X, 64, 0, 0); 22 } 23 else if(three == 2) 24 { 25 al_draw_bitmap(O, 64, 0, 0); 26 } 27 if(four == 1) 28 { 29 al_draw_bitmap(X, 0, 32, 0); 30 } 31 else if(four == 2) 32 { 33 al_draw_bitmap(O, 0, 32, 0); 34 } 35 if(five == 1) 36 { 37 al_draw_bitmap(X, 32, 32, 0); 38 } 39 else if(five == 2) 40 { 41 al_draw_bitmap(O, 32, 32, 0); 42 } 43 if(six == 1) 44 { 45 al_draw_bitmap(X, 64, 32, 0); 46 } 47 else if(six == 2) 48 { 49 al_draw_bitmap(O, 64, 32, 0); 50 } 51 if(seven == 1) 52 { 53 al_draw_bitmap(X, 0, 64, 0); 54 } 55 else if(seven == 2) 56 { 57 al_draw_bitmap(O, 0, 64, 0); 58 } 59 if(eight == 1) 60 { 61 al_draw_bitmap(X, 32, 64, 0); 62 } 63 else if(eight == 2) 64 { 65 al_draw_bitmap(O, 32, 64, 0); 66 } 67 if(nine == 1) 68 { 69 al_draw_bitmap(X, 64, 64, 0); 70 } 71 else if(nine == 2) 72 { 73 al_draw_bitmap(O, 64, 64, 0); 74 } 75 76 al_flip_display(); 77 }

But the timer solution is more robust.

EDIT: Another option is to only render when:
-The window loses focus
-The window gains focus
-A mouse event

This would, in general consume less cpu than the timer solution, but, timers are still better in 2014.

Antone333
Member #15,545
March 2014

so i tried using al_is_event_queue_empty() and it allows me to place the first x but then after that the program freezes. or appears to freeze at least. because it is still running just nothing happens.

next what i was going to try was the timer.

but also does not work. it gives the same results as the empty queue

int fps = 60;

ALLEGRO_TIMER *timer = NULL;

timer = al_create_timer(1.0/FPS);

al_register_event_source(EventQueue, al_get_timer_event_source(timer));

al_start_timer(timer);
while(!Exit)
{
if(button click)
{all the button clicks}

if(Event.type == ALLEGRO_EVENT_TIMER)
{
if(one == 1)
{
al_draw_bitmap(X, 0, 0, 0);
}
else if(one == 2)
{
al_draw_bitmap(O, 0, 0, 0);
}
if(two == 1)
{
al_draw_bitmap(X, 32, 0, 0);
}
else if(two == 2)
{
al_draw_bitmap(O, 32, 0, 0);
}
if(three == 1)
{
al_draw_bitmap(X, 64, 0, 0);
}
else if(three == 2)
{
al_draw_bitmap(O, 64, 0, 0);
}
if(four == 1)
{
al_draw_bitmap(X, 0, 32, 0);
}
else if(four == 2)
{
al_draw_bitmap(O, 0, 32, 0);
}
if(five == 1)
{
al_draw_bitmap(X, 32, 32, 0);
}
else if(five == 2)
{
al_draw_bitmap(O, 32, 32, 0);
}
if(six == 1)
{
al_draw_bitmap(X, 64, 32, 0);
}
else if(six == 2)
{
al_draw_bitmap(O, 64, 32, 0);
}
if(seven == 1)
{
al_draw_bitmap(X, 0, 64, 0);
}
else if(seven == 2)
{
al_draw_bitmap(O, 0, 64, 0);
}
if(eight == 1)
{
al_draw_bitmap(X, 32, 64, 0);
}
else if(eight == 2)
{
al_draw_bitmap(O, 32, 64, 0);
}
if(nine == 1)
{
al_draw_bitmap(X, 64, 64, 0);
}
else if(nine == 2)
{
al_draw_bitmap(O, 64, 64, 0);
}
}
}

jmasterx
Member #11,410
October 2009

Not quite,

you need a bool needsRedraw;

When you get a timer event, set needs redraw to true.

Then in your main loop, right after you wait for event, do:

if(event queue is empty and needs redraw)
{
   clear the screen,
     draw
     flip
set needsRedraw to false
}

Antone333
Member #15,545
March 2014

so for the bool redraw. i set every time the mouse button is pressed down it makes redraw true?

jmasterx
Member #11,410
October 2009

You could do that if you only ever plan to have that as your only trigger. Otherwise, the timer will ensure it happens 60 times per second if you set it true in there. I recommend the timer since you might want to add animation in the future.

Antone333
Member #15,545
March 2014

so would i do this?

when i try that i now just get a black screen.

if(Event.type == ALLEGRO_EVENT_TIMER && Redraw == true)

jmasterx
Member #11,410
October 2009

No,

al_wait_for_event
if(Event.type == ALLEGRO_EVENT_TIMER)
{
   redraw = true;
} 
else if(mouse event)
{
}

if(al_event_queue_empty && redraw)
{
   //do all drawing code
   redraw = false;
}

The logic is:
if it has been at least 17ms, you have permission to redraw yourself.
if there are no events in the queue and you have permission to redraw yourself, then redraw yourself and revoke your permission to draw yourself.

Just a little tip,
instead of if(x == true) and if(x == false) you can write: if(x) and if(!x)

Also, if you have code, then put it in code blocks:

<code>
your code
</code>

Antone333
Member #15,545
March 2014

getting there? it is still pretty spotty with it working.

seems like its working a little bit better though.

else if(ev.mouse.button & 1 && PTwo == 1 && nine == 0 && Event.mouse.x >= 64 && Event.mouse.x <= 96 && Event.mouse.y >= 64 && Event.mouse.y <= 96)
{
nine = 2;
POne = 1;
PTwo = 0;
}
else if(ev.mouse.button & 1 && Event.mouse.x >= 32 && Event.mouse.x <= 64 && Event.mouse.y >= 96 && Event.mouse.y <= 128)
{
Exit = true;
Restart = true;
}
}

if(Event.type == ALLEGRO_EVENT_TIMER)
{
Redraw = true;
}

if(al_is_event_queue_empty(EventQueue) && Redraw)
{
if(one == 1)
{
al_draw_bitmap(X, 0, 0, 0);
}

jmasterx
Member #11,410
October 2009

Could you try to integrate your code into this main.cpp template I have; I know this template works; Just that it's a little old:

Comment out whatever you do not need.

#SelectExpand
1/*====================================== 2ALLEGRO 5 TEMPLATE 3======================================== */ 4 5#include <stdlib.h> //Standard library 6#include <string.h> //String functions 7#include <sstream> //More string functions 8 9#include <allegro5/allegro.h> 10#include <allegro5/allegro5.h> 11#include <allegro5/allegro_image.h> 12#include <allegro5/allegro_primitives.h> 13#include <allegro5/allegro_audio.h> 14#include <allegro5/allegro_font.h> 15#include <allegro5/allegro_ttf.h> 16#include <allegro5/allegro_vorbis.h> 17#include <allegro5/allegro_flac.h> 18#include <allegro5/allegro_opengl.h> 19#include <allegro5/allegro_physfs.h> 20//#include <physfs.h> 21 22#include <Windows.h> 23 24#include <vector> //Dynamic array data structure 25#include <queue> //Queue FIFO data structure 26#include <cmath> // C Math functions 27#include <ctime> // C Time functions 28 29#define FRAME_RATE 60 30 31//Declarations 32ALLEGRO_DISPLAY *display; 33ALLEGRO_MONITOR_INFO info; 34ALLEGRO_TIMER *timer; 35bool done; 36//color 37ALLEGRO_COLOR BLACK = al_map_rgb(0,0,0); 38 39//Keyboard 40bool key[256]; 41 42//Mouse 43int mX = 0; 44int mY = 0; 45 46void init() { 47 //Install event handlers 48 al_init(); 49 al_init_image_addon(); 50 al_init_font_addon(); 51 al_init_ttf_addon(); 52 al_install_mouse(); 53 al_install_keyboard(); 54 al_install_joystick(); 55 al_install_audio(ALLEGRO_AUDIO_DRIVER_AUTODETECT); 56 al_init_ogg_vorbis_addon(); 57 58 //For PhysFS 59 //PHYSFS_init(0); 60 //PHYSFS_addToSearchPath("",1); 61 62 // Start a timer to regulate speed 63 64 timer = al_install_timer(1.0/FRAME_RATE); 65 al_start_timer(timer); 66 67 //for audio.... 68 al_reserve_samples(64); 69 70 //for keyboard... 71 memset(key,0,sizeof(key)); 72 73 //show the mouse 74 al_show_mouse_cursor(); 75 76 //make the random function randomer 77 srand(time(NULL)); 78 79 80 //show screen 81 al_set_new_display_flags(ALLEGRO_RESIZABLE); 82 display = al_create_display(640, 480); 83 84 //For PhysFS 85 //al_set_physfs_file_interface(); 86 87 //Window Title 88 al_set_window_title("Allegro 5 Template"); 89 90 91 92 93} 94 95//Mouse_Down 96void mousedown(ALLEGRO_MOUSE_EVENT *mouse) { 97 98 } 99//Mouse Up 100void mouseup(ALLEGRO_MOUSE_EVENT *mouse) { 101 102 } 103 104// If a key is pressed down, add it to the key array 105void keydown(ALLEGRO_KEYBOARD_EVENT *kb) { 106 key[kb->keycode] = true; 107 108 109 if(kb->keycode == ALLEGRO_KEY_ESCAPE) { 110 111 done = true; 112 } 113} 114 115// If a key is released, mark it as unpressed 116void keyup(ALLEGRO_KEYBOARD_EVENT *kb) { 117 key[kb->keycode] = false; 118} 119 120// If an operating system repeat event comes in, set the flag 121void keyrepeat(ALLEGRO_KEYBOARD_EVENT *kb) { 122 123} 124 125//Mouse Move 126 127void mouseaxes(ALLEGRO_MOUSE_EVENT *mouse) { 128 mX = mouse->x; 129 mY = mouse->y; 130 131 } 132//RENDER CURRENT FRAME 133 void render() 134 { 135 //draw to the screen 136 al_clear_to_color(al_map_rgb(0,0,0)); 137 //code goes here 138 al_flip_display(); 139 140 } 141 142int main(int argc, char *argv[]) 143 144{ 145 //initialize game 146 init(); 147 bool need_redraw = true; 148 149 //Main Loop 150 151//***** Start Main Code Here ***** 152 153 // Start the event queue to handle keyboard input and our timer 154 ALLEGRO_EVENT_QUEUE *queue = al_create_event_queue(); 155 al_set_new_bitmap_flags(ALLEGRO_VIDEO_BITMAP); 156 al_register_event_source(queue, (ALLEGRO_EVENT_SOURCE*)al_get_keyboard_event_source()); 157 al_register_event_source(queue, (ALLEGRO_EVENT_SOURCE*)al_get_mouse_event_source()); 158 al_register_event_source(queue, (ALLEGRO_EVENT_SOURCE*)timer); 159 al_register_event_source(queue, (ALLEGRO_EVENT_SOURCE*)display); 160 ALLEGRO_EVENT event; 161 while(!done) { 162 163 // Block until an event enters the queue 164 if (need_redraw && al_event_queue_is_empty(queue)) { 165 render(); 166 need_redraw = false; 167 } 168 al_wait_for_event(queue, &event); 169 170 //We only get here if there is an event in the queue 171 switch(event.type) { 172 case ALLEGRO_EVENT_MOUSE_AXES: 173 mouseaxes(&event.mouse); 174 break; 175 176 case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN: 177 mousedown(&event.mouse); 178 break; 179 180 case ALLEGRO_EVENT_MOUSE_BUTTON_UP: 181 mouseup(&event.mouse); 182 break; 183 184 case ALLEGRO_EVENT_KEY_DOWN: 185 keydown(&event.keyboard); 186 break; 187 188 case ALLEGRO_EVENT_KEY_UP: 189 keyup(&event.keyboard); 190 break; 191 192 case ALLEGRO_EVENT_KEY_REPEAT: 193 keyrepeat(&event.keyboard); 194 break; 195 196 case ALLEGRO_EVENT_TIMER: 197 need_redraw = true; 198 break; 199 case ALLEGRO_EVENT_DISPLAY_RESIZE: 200 al_acknowledge_resize(event.display.source); 201 break; 202 case ALLEGRO_EVENT_DISPLAY_CLOSE: 203 return 0; 204 break; 205 } 206 207 208 } 209//***** End Main Code Here ***** 210 211 212 return 0; 213}

Antone333
Member #15,545
March 2014

sure. i could do that.

I will work on it later today. i have to go for now. probably have it finished by tomorrow morning. I greatly appreciate all your trying to help and hope we can continue talking when i get around to finishing this.

jmasterx
Member #11,410
October 2009

Ok great. I'm just typing so you can respond when you're done.

Antone333
Member #15,545
March 2014

already came to a problem though. at first i compiled it and got errors from

//#include <allegro5/allegro_vorbis.h>
//#include <allegro5/allegro_flac.h>
//#include <physfs.h>

because it could not find them and errors from im assuming everything that use those headers. so i commented them out. program runs but all that comes up is console window and a message box that says "Project1.exe has stopped working" a problem caused it to stop working bla bla bla

jmasterx
Member #11,410
October 2009

Hi

Here is a revised version of the template that I just tested and works perfectly:

#SelectExpand
1/*====================================== 2ALLEGRO 5 TEMPLATE 3======================================== */ 4 5#include <stdlib.h> //Standard library 6#include <string.h> //String functions 7#include <sstream> //More string functions 8 9#include <allegro5/allegro.h> 10#include <allegro5/allegro5.h> 11#include <allegro5/allegro_image.h> 12#include <allegro5/allegro_primitives.h> 13#include <allegro5/allegro_audio.h> 14#include <allegro5/allegro_font.h> 15#include <allegro5/allegro_ttf.h> 16#include <allegro5/allegro_opengl.h> 17 18#include <vector> //Dynamic array data structure 19#include <queue> //Queue FIFO data structure 20#include <cmath> // C Math functions 21#include <ctime> // C Time functions 22 23#define FRAME_RATE 60 24 25//Declarations 26ALLEGRO_DISPLAY *display; 27ALLEGRO_MONITOR_INFO info; 28ALLEGRO_TIMER *timer; 29bool done; 30//color 31ALLEGRO_COLOR BLACK = al_map_rgb(0,0,0); 32 33//Keyboard 34bool key[256]; 35 36//Mouse 37int mX = 0; 38int mY = 0; 39 40void init() { 41 //Install event handlers 42 al_init(); 43 al_init_image_addon(); 44 al_init_font_addon(); 45 al_init_ttf_addon(); 46 al_install_mouse(); 47 al_install_keyboard(); 48 49 //For PhysFS 50 //PHYSFS_init(0); 51 //PHYSFS_addToSearchPath("",1); 52 53 // Start a timer to regulate speed 54 55 timer = al_create_timer(1.0/FRAME_RATE); 56 al_start_timer(timer); 57 58 //for keyboard... 59 memset(key,0,sizeof(key)); 60 61 //show the mouse 62 al_show_mouse_cursor(display); 63 64 //make the random function randomer 65 srand(time(NULL)); 66 67 68 //show screen 69 display = al_create_display(640, 480); 70 71 //For PhysFS 72 //al_set_physfs_file_interface(); 73 74 //Window Title 75 al_set_window_title(display,"Allegro 5 Template"); 76} 77 78//Mouse_Down 79void mousedown(ALLEGRO_MOUSE_EVENT *mouse) { 80 81 } 82//Mouse Up 83void mouseup(ALLEGRO_MOUSE_EVENT *mouse) { 84 85 } 86 87// If a key is pressed down, add it to the key array 88void keydown(ALLEGRO_KEYBOARD_EVENT *kb) { 89 key[kb->keycode] = true; 90 if(kb->keycode == ALLEGRO_KEY_ESCAPE) { 91 92 done = true; 93 } 94} 95 96// If a key is released, mark it as unpressed 97void keyup(ALLEGRO_KEYBOARD_EVENT *kb) { 98 key[kb->keycode] = false; 99} 100 101// If an operating system repeat event comes in, set the flag 102void keyrepeat(ALLEGRO_KEYBOARD_EVENT *kb) { 103 104} 105 106//Mouse Move 107 108void mouseaxes(ALLEGRO_MOUSE_EVENT *mouse) { 109 mX = mouse->x; 110 mY = mouse->y; 111 112 } 113//RENDER CURRENT FRAME 114 void render() 115 { 116 //draw to the screen 117 al_clear_to_color(al_map_rgb(0,0,0)); 118 //code goes here 119 al_flip_display(); 120 121 } 122 123int main(int argc, char *argv[]) 124 125{ 126 //initialize game 127 init(); 128 bool need_redraw = true; 129 130 //Main Loop 131 132//***** Start Main Code Here ***** 133 134 // Start the event queue to handle keyboard input and our timer 135 ALLEGRO_EVENT_QUEUE *queue = al_create_event_queue(); 136 al_set_new_bitmap_flags(ALLEGRO_VIDEO_BITMAP); 137 al_register_event_source(queue, (ALLEGRO_EVENT_SOURCE*)al_get_keyboard_event_source()); 138 al_register_event_source(queue, (ALLEGRO_EVENT_SOURCE*)al_get_mouse_event_source()); 139 al_register_event_source(queue, (ALLEGRO_EVENT_SOURCE*)timer); 140 al_register_event_source(queue, (ALLEGRO_EVENT_SOURCE*)display); 141 ALLEGRO_EVENT event; 142 while(!done) { 143 144 // Block until an event enters the queue 145 if (need_redraw && al_event_queue_is_empty(queue)) { 146 render(); 147 need_redraw = false; 148 } 149 al_wait_for_event(queue, &event); 150 151 //We only get here if there is an event in the queue 152 switch(event.type) { 153 case ALLEGRO_EVENT_MOUSE_AXES: 154 mouseaxes(&event.mouse); 155 break; 156 157 case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN: 158 mousedown(&event.mouse); 159 break; 160 161 case ALLEGRO_EVENT_MOUSE_BUTTON_UP: 162 mouseup(&event.mouse); 163 break; 164 165 case ALLEGRO_EVENT_KEY_DOWN: 166 keydown(&event.keyboard); 167 break; 168 169 case ALLEGRO_EVENT_KEY_UP: 170 keyup(&event.keyboard); 171 break; 172 case ALLEGRO_EVENT_TIMER: 173 need_redraw = true; 174 break; 175 case ALLEGRO_EVENT_DISPLAY_RESIZE: 176 al_acknowledge_resize(event.display.source); 177 break; 178 case ALLEGRO_EVENT_DISPLAY_CLOSE: 179 return 0; 180 break; 181 } 182 183 184 } 185//***** End Main Code Here ***** 186 187 188 return 0; 189}

The crash came from the line:
al_reserve_samples(64) which now requires a default mixer to work.

Antone333
Member #15,545
March 2014

OK so i spent a good two hours last night and the template was proving to be way beyond me. but i will keep trying. It runs quite a bit better. but still not exactly right.

I put my code into this:
https://wiki.allegro.cc/index.php?title=Allegro_5_Tutorial/Timers

Thomas Fjellstrom
Member #476
June 2000
avatar

feel free to ask questions about the stuff you don't understand. it may make good material for the wiki or a faq. :)

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Antone333
Member #15,545
March 2014

well yeah but im very much a beginner with allegro 5 and am by no means good with general c++ so the stuff i dont understand is like half the template jmasterx has posted.

Thomas Fjellstrom
Member #476
June 2000
avatar

Well, after you have a shot at figuring out, if you still have questions, you're always welcome to ask :)

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Antone333
Member #15,545
March 2014

Yeah i for sure will.

I am quite happy with my tic tac toe game as it is right now. it works out moderately well. not 100% but i will keep working on it.

Edit:
Just made some new parts to the board. i added a menu icon that does nothing. i mean its tic tac toe. but maybe ill use it for something eventually. and a quit button

jmasterx
Member #11,410
October 2009

Yeah, that's cool :)

Like Thomas said, if you have any questions, we are all happy to answer. The template breaks things down into separate functions to keep the code more organized. You might not be familiar with functions yet and that's fine. With some more practice you will get the hang of it.

One of my first programs was a tic-tac-toe with A.I in vb .net, maybe you can eventually look into making a computer player for it.

Thomas Fjellstrom
Member #476
June 2000
avatar

jmasterx said:

One of my first programs was a tic-tac-toe with A.I in vb .net

vb.net? GET OFF MY LAWN. damn kids. >:(

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

jmasterx
Member #11,410
October 2009

Yup, I learned programming in this order:
VB .NET -> C++/C -> C# -> SQL -> JavaScript -> Java -> PHP -> Objective-C.

I have not written anything in VB .NET in nearly 5 years though, but hey, you have to start somewhere; we young whipper-snappers can't imagine starting with assembly or cobol ;)

6 years ago I never imagined myself being able to program, but, one day I decided I wanted to learn it and I became really good at it quite quickly.

Antone333
Member #15,545
March 2014

This is another game i have been working on. I need to really revise it though. What i need done is completely redo the inventory and bank system to make the items structures instead. only problem is im not fantastic with dealing with structures. but how i have it works... its just hard to add new items. tic tac toe was getting started with graphics so i can hopefully make this rpg using graphics. the character and tiles are what i made to try and get into that game.

And also something i think i should add. i am going to be starting my senior year of highschool and will be taking robotics. we will be using robotc vex sets. After highschool i plan on going to san francisco state university to study computer science with software engineering base.

jmasterx
Member #11,410
October 2009

Nice!

From my own experience of being in a computer science program, still continue to do as much hobby programming as you can. I find the types of problems you run into with school projects are not always representative of the problems you face as a professional software developer.In Uni, you can tell the students that do hobby programming, and those that just do the assignments.

Go to: