Allegro

Pages: 1234
Is there any way i can make a a mosueclick recodnise a drawn opject whitch was drawn by command not uisng a sprite. i mean, i use command
1
2
circlefill ( buffer, cursor_x, cursor_y, 20, makecol( 0, 0, 255));
     draw_sprite( screen, buffer, 0, 0);
and then later if i click on the circle is there anyway for it to find the x and y coords of circles middle point? I want to create a selection tool. I use one click to draw a object nad the other click to select it and thenif its selecdted to move it around.
Last edited on
Don't really know allegro, but it hardly matters. Any way, you'll need to remember the coordinates of all of your triangles (probably in a vector). Then, when the mouse is clicked, iterate through them all and choose the one nearest to the cursor.
so, i need it to remember all the locations, with radius, and when i klick on the object, then i searches for an object nearest to it, and if it is within the radius, then it will select the object. yes?
yes
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
#include <allegro.h>
#include <string.h>
#include <string>

BITMAP* buffer;
int cursor_x = 20;
int cursor_y = 20;
int radius = 30;
char *asukoht;
void formatdata() {
     asukoht[0] = cursor_x;
     asukoht[1] = cursor_y;
     asukoht[2] = radius;
     }
int getMouseInfo(){
     if(mouse_b & 1){
                  cursor_x = mouse_x;
                  cursor_y = mouse_y;
      return 1;
     }
  return 0;
}
void updateScreen(){
 
     show_mouse(NULL);
     circlefill ( buffer, cursor_x, cursor_y, radius, makecol( 0, 0, 255));
     draw_sprite( screen, buffer, 0, 0); 
     formatdata();
     for(int i=0; i < 3; i++){
            textprintf(screen, font, 0, 10, makecol(255, 255, 255), "An integer: %s", asukoht);           
             }
}
int main(){
    
    allegro_init();
    install_mouse();
    install_keyboard();
    set_color_depth(16);
    set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0);
    
    buffer = create_bitmap( 640, 480);     

    show_mouse(screen);        
 
    while( !key[KEY_ESC])
 {
  int switcher=1;
  while(getMouseInfo()) 
  { 
   updateScreen();
   if(getMouseInfo()==0) switcher=0;
  }
  if(switcher==0) show_mouse(screen);
    }
    
 return 0; 
}
END_OF_MAIN();

Im sure i have done somethin wrong, but i just cannot find what. When i run this program, i make a click, i get a blue circle and then it crashes. I wanted it to print the coordination int the cornenr of the screen so that i could see if it does it right. can someone help me?
asukoht points to nothing. I suggest that you use vectors. Something like this.
1
2
3
4
5
6
//declarations
struct Circle{
   int x, y, radius;
};
std::vector<Circle> scene;
int selected = -1;


1
2
3
4
5
//drawing code
for(int i = 0; i < scene.size(); i++){
   //draw a circle at coordinates scene[i].x, scene[i].y and with radius of scene[i].radius
   if (i == selected) //mark somehow the selected circle
}


1
2
3
4
5
6
//input code (selection)
selected = -1;//so that a random click deselects
for(int i = 0; i < scene.size(); i++){
   if(distance(cursor_x-scene[i].x, cursor_y-scene[i].y) <= scene[i].radius) selected = i;
}
//here distance(x, y) = sqrt(x*x+y*y); 


1
2
3
4
5
6
//input code (creation)
Circle c;
c.x = cursor_x;
c.y = cursor_y;
c.radius = 10;
scene.push_back(c);
Last edited on
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
struct Circle{
       int x, y, radius;
       };
       std::vector<Circle> scene;
       int selected = -1;
int getMouseInfo(){
     if(mouse_b & 1){
                  cursor_x = mouse_x;
                  cursor_y = mouse_y;
      return 1;
     }
  return 0;
}
void updateScreen(){
 
     show_mouse(NULL);
     for(int i = 0; i <scene.size(); i++){ 
             scene[i].x = cursor_x;
             scene[i].y = cursor_y;
             scene[i].radius = radius;
     circlefill ( buffer, cursor_x, cursor_y, radius, makecol( 0, 0, 255));
     if(i == selected){
          selected = -1;
for(int i = 0; i < scene.size(); i++){
   if(distance(cursor_x-scene[i].x, cursor_y-scene[i].y) <= scene[i].radius) selected = i;
}
          rect(buffer, cursor_x, cursor_y, (radius + 10), (radius + 10),makecol( 0, 0, 255));
          }
          }
     draw_sprite( screen, buffer, 0, 0); 
     
}

i probably messed it up, cause i have never uesd vector before and i probably messed something else up do.
Why reassign coordinates? They were saved to remain constant. If you want one circle to be on your cursor, either do it separately, or only reset scene[selected] (assuming that selected is not -1).
Then, why draw all circles on the cursor instead of scene[i].x, .y? Maybe I misunderstood what you wanted...
Also, why put the code I named as input into the code I called drawing? That one is supposed to be in whatever function gets called when you press a mouse button.

Let's try doing less. Ignore the mouse for now. Write a program that draws all contents of scene. Put a few Circles into scene vector while initializing. I hope that will make you more familiar with the model I'm suggesting..
ugh.... This would be lot easyer if englis were my native tongue. So if i understood correctly. I will but some circles into the scene(i write it into the code), and then it just draws them onto the screen.
Last edited on
std::vector<Circle> scene;
expected constructor, destructor, or type conversion before '<' token
expected `,' or `;' before '<' token

what now.
Weird.. Are you including <vector> ? Post more of your code.
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
struct Circle{
       int x, y, radius;
       };
       std::vector<Circle> scene;
       int selected = -1;
       Circle c;
c.x = 300;
c.y = 300;
c.radius = radius;

int getMouseInfo(){
     if(mouse_b & 1){
                  cursor_x = mouse_x;
                  cursor_y = mouse_y;
      return 1;
     }
  return 0;
}
void updateScreen(){
 
     show_mouse(NULL);
   
     circlefill ( buffer, c.x, c.y, c.radius, makecol( 0, 0, 255));
     draw_sprite( screen, buffer, 0, 0); 
     
}


im 100% sure im douing something wrong. i tryed with c.x and i tryed with scene.x i got the same message, so i have no ide whats wrong
Well, lines 7-9 are a problem. Only initialization can be done in global scope. Either move these lines into main() or change 6 to Circle c = {300, 300, radius};

The bit with vector is okay, assuming that <vector> is included. See for yourself - http://ideone.com/llGu2

ok i got it working. Now the code goes like this: I start the program, i get black screen. I press left mouse button and a circle appears in 300, 300 with a radius of radius. Now, whats the next step. Il make a array or something to record the locations and radiuses of all the circles i make with mouse click??
Like I said, ignore the mouse for now. In main, write
1
2
3
4
Circle tmp1 = {100, 100, 20}, tmp2 = {200, 100, 30}, tmp3 = {150, 150, 40};
scene.push_back(tmp1);
scene.push_back(tmp2);
scene.push_back(tmp3);


And in wherever you redraw your screen, iterate through scene and draw all circles in it.
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
#include <allegro.h>
#include <vector>

BITMAP* buffer;
int cursor_x = 20;
int cursor_y = 20;
int radius = 30;
struct Circle{
       int x, y, radius;
       };
       std::vector<Circle> scene;
       int selected = -1;
       Circle c = {300, 300, radius};


//int getMouseInfo(){
  //   if(mouse_b & 1){
    //              cursor_x = mouse_x;
      //            cursor_y = mouse_y;
     // return 1;
    // }
 // return 0;
//}
void updateScreen(){
 
   //  show_mouse(NULL);
   for(int i = 0; i < scene.size(); i++){
     circlefill ( buffer, scene[i].x, scene[i].y, scene[i].radius, makecol( 0, 0, 255));
     draw_sprite( screen, buffer, 0, 0); 
     }
     
}
int main(){
    
    allegro_init();
  //  install_mouse();
    install_keyboard();
    set_color_depth(16);
    set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0);
    
    buffer = create_bitmap( 640, 480);     

Circle tmp1 = {100, 100, 20}, tmp2 = {200, 100, 30}, tmp3 = {150, 150, 40};
scene.push_back(tmp1);
scene.push_back(tmp2);
scene.push_back(tmp3);

  //  show_mouse(screen);        
 
    while( !key[KEY_ESC])
 {
 // int switcher=1;
  //while(getMouseInfo()) 
  //{ 
   updateScreen();
   //if(getMouseInfo()==0) switcher=0;
 // }
  //if(switcher==0) show_mouse(screen);
    }
    
 return 0; 
}
END_OF_MAIN();


ok so it has 3 circles in scene and it iterates through it and draws em all. I think im starting to get what you are aiming at. but im not sure.
Last edited on
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
#include <allegro.h>
#include <vector>

BITMAP* buffer;
int cursor_x = 20;
int cursor_y = 20;
int radius = 30;
struct Circle{
       int x, y, radius;
       };
       std::vector<Circle> scene;
       int selected = -1;
       Circle c = {300, 300, radius};


int getMouseInfo(){
   if(mouse_b & 1){
                  cursor_x = mouse_x;
                  cursor_y = mouse_y;
                  Circle c;
c.x = cursor_x;
c.y = cursor_y;
c.radius = 10;
scene.push_back(c);
      return 1;
     }
  return 0;
}
void updateScreen(){
 
    show_mouse(NULL);
     for(int i = 0; i < scene.size(); i++){
     circlefill ( buffer, cursor_x, cursor_y, radius, makecol( 0, 0, 255));
     //just for testing
     circlefill ( buffer, scene[i].x + 100, scene[i].y + 100, scene[i].radius, makecol(0, 0, 255));
     draw_sprite( screen, buffer, 0, 0); 
     }
     
}
int main(){
    
    allegro_init();
    install_mouse();
    install_keyboard();
    set_color_depth(16);
    set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0);
    
    buffer = create_bitmap( 640, 480);     
    
    show_mouse(screen);        

    while( !key[KEY_ESC])
 {
  int switcher=1;
  while(getMouseInfo()) 
 { 
   updateScreen();
 if(getMouseInfo()==0) switcher=0;
  }
  if(switcher==0) show_mouse(screen);
    }
    
 return 0; 
}
END_OF_MAIN();


ok so this one is working also. I click the button, it create a circle, nd then creates a smaller circle near it. So whats next. Oh and if i hold mouse button down and drag it it has lagg.
Last edited on
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
#include <allegro.h>
#include <vector>
#include <math.h>

BITMAP* buffer;
int cursor_x = 20;
int cursor_y = 20;
int radius = 30;

double distance(double x , double y){
       double Z = sqrt((x*x) + (y*y));
       return Z;
       }
struct Circle{
       int x, y, radius;
       };
       std::vector<Circle> scene;
       int selected = -1;
       Circle c = {300, 300, radius};


int getMouseInfo(){
   if(mouse_b & 1){
                  cursor_x = mouse_x;
                  cursor_y = mouse_y;
                  Circle c;
c.x = cursor_x;
c.y = cursor_y;
c.radius = radius;
scene.push_back(c);
      return 1;
     }
     if (mouse_b & 2){
                 cursor_x = mouse_x;
                 cursor_y = mouse_x;
                 return 1;
                 }
  return 0;
}
void updateScreen(){
 
    show_mouse(NULL);
    int g;
    selected = -1;
     for(int i = 0; i < scene.size(); i++){  
           if (distance(cursor_x-scene[i].x, cursor_y-scene[i].y)  <= scene[i].radius){
                                              g = selected;
                                             
     if( g != selected){                                                                   
     circlefill ( buffer, cursor_x, cursor_y, radius, makecol( 0, 0, 255));
     }
     else if(g == selected){
          rect(buffer, scene[i].x - scene[i].radius - 10, scene[i].y - scene[i].radius - 10,
           scene[i].x + scene[i].radius + 10, scene[i].y + scene[i].radius + 10, makecol(0,0,255));
          }
          }
     draw_sprite( screen, buffer, 0, 0); 
     }
     
}
int main(){
    
    allegro_init();
    install_mouse();
    install_keyboard();
    set_color_depth(16);
    set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0);
    
    buffer = create_bitmap( 640, 480);     
    
    show_mouse(screen);        

    while( !key[KEY_ESC])
 {
  int switcher=1;
  while(getMouseInfo()) 
 { 
   updateScreen();
 if(getMouseInfo()==0) switcher=0;
  }
  if(switcher==0) show_mouse(screen);
    }
    
 return 0; 
}
END_OF_MAIN();


Last try for today. I know something is wrong, but i cant tell what.
Last edited on
About your previous code, when you hold and drag, new circles are pushed on every frame. That's a lot of circles..

About your latest one, you've combined your selection code with drawing code. Selection has to be moved to line 35. The rest seems good, though I may miss things as I'm only looking through it.

In the future, you'll need to improve your mouse handling. You should be able to tell whether a button is pressed for the first time, or it was down the last frame too. You'll need a variable to remember it's prior state.
Also, you only update cursor position when the mouse has been clicked. You'll need to know where the cursor is constantly, when you do dragging. Though you can just use mouse_x/y anywhere, can't you?
Make your question more comprehensible.
Pages: 1234