Allegro.cc - Online Community

Allegro.cc Forums » Allegro Development » Unexpected error in primitive al_draw_circle (aka Return Of The Bad Programmer)

This thread is locked; no one can reply to it. rss feed Print
Unexpected error in primitive al_draw_circle (aka Return Of The Bad Programmer)
mumu1000
Member #16,842
April 2018

Hello everybody ! :D
Once again i come back to you with a simple/weird (stupid?) question, and a new bug i cant figure out how to fix. :P

here's my code:

main.cpp

#SelectExpand
1#include <iostream> 2#include <string> 3#include <chrono> 4#include <thread> 5#include "allegro5/allegro.h" 6#include "allegro5/allegro_color.h" 7#include "allegro5/allegro_primitives.h" 8 9int main(int argc, char **argv) 10{ 11 if (al_init() && al_init_primitives_addon()) 12 { 13 std::cout<<"It Works"; 14 }else 15 { 16 std::cout << "Allegro Not Loaded"; 17 return 1; 18 } 19 al_set_new_display_flags(ALLEGRO_FRAMELESS+ALLEGRO_FULLSCREEN_WINDOW); 20 ALLEGRO_DISPLAY * mainDisplay = al_create_display(1800,1000); 21 22 al_clear_to_color(al_map_rgb(20,20,20)); 23 Planet* testPlanet = new Planet(100,100); 24 testPlanet->draw(); 25 al_flip_display(); 26 std::this_thread::sleep_for(std::chrono::milliseconds(5000)); 27 28 al_shutdown_primitives_addon(); 29 al_destroy_display(mainDisplay); 30 return 0; 31}

Planet.h

#SelectExpand
1#include "allegro5/color.h" 2#include "allegro5/allegro_primitives.h" 3class Planet 4{ 5 public: 6 Planet(); 7 Planet(float xPos, float yPos); 8 virtual ~Planet(); 9 virtual void const draw(); 10 float const getXPos(){return m_xPos;}; 11 float const getYPos(){return m_yPos;}; 12 void setXPos(float xPos){m_xPos = xPos;}; 13 void setYPos(float yPos){m_yPos = yPos;}; 14 15 protected: 16 float m_xPos; 17 float m_yPos; 18 19 private: 20 const static ALLEGRO_COLOR drawedColor; 21};

Planet.cpp

#SelectExpand
1#include "Planet.h" 2 3const ALLEGRO_COLOR Planet::drawedColor = al_map_rgb(20,200,20); 4 5Planet::Planet() 6{ 7 m_xPos = 0; 8 m_yPos = 0; 9} 10Planet::Planet(float xPos, float yPos) 11{ 12 m_xPos = xPos; 13 m_yPos = yPos; 14} 15 16Planet::~Planet() 17{ 18 19} 20void const Planet::draw() 21{ 22 al_draw_circle(10+(m_xPos*30),10+(m_yPos*30),8,Planet::drawedColor,3); 23}

What i figured out :
-The code compiles.
-The display opens, and i got the "It works" message in the console.
-The display stays black, and i get an error code after a few secs (0XC0000005)
-When i remove the al_draw_circle call, the code runs, and returns 0.
-When i call al_draw_circle from the main, it works.

Please help ! :'(

Have a great day !

Edit: The problem is not coming from here (i simplified the problem in this forum thread, but i think the problem comes from another part of the code)7

Edit 2: This is so confusing, i cant get it to work. Do you see any mistakes i've made in these sources ?

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

I see one thing. You're using a global const ALLEGRO_COLOR and caling al_map_rgb outside of al_init() because global constructors get called before main.

Calling allegro functions before al_init can give undefined behavior.

EDIT
Also, that code won't compile because you didn't #include "planet.h".

Post a minimal working example that compiles if the above didn't fix the problem.

mumu1000
Member #16,842
April 2018

I see one thing. You're using a global const ALLEGRO_COLOR and caling al_map_rgb outside of al_init() because global constructors get called before main.

Calling allegro functions before al_init can give undefined behavior.

Oh ! Very interesting ! Thx for the answer !

Edgar Reynaldo said:

Also, that code won't compile because you didn't #include "planet.h"

Oh yea my bad, this error comes from when i tried to synthetize the problem and refactored the code to describe my problem better. (As i told, my code compiles ^^)

Edgar Reynaldo said:

Post a minimal working example that compiles if the above didn't fix the problem.

This example is not relevant anymore, i've made a new post, with all the actual sources, and one new hell of an issue ;p

Great day !

Go to: