![]() |
|
Basic menu idea? |
Tquila
Member #13,741
November 2011
|
I've been looking around for this, but havn't quite found anything. My own idea is something like making some text (Like "Play") and then detect if the mouse is on it and then begin game loop from there (or draw another screen if it's High Scores or whatever). This just seems a bit weird seeing you would do some "collision" detection with the mouse and the text. Is there some simple "normal best-practice go-to" way to do this? Please note I write in C (forced) so classes is not an option (albeit I hardly can see how in this case). Edit: (totally OT) how would you scroll in a game enviroment? If you have a screen say 640x480, but a game board of 2560x1920, how would you make that scroll? My (again I'm so good at pseudocoding this >.<) idea is that you do either of: 1: Center fix your character, and use X / Y coords to determain where on the board you are. But how would you do the actual scrolling? But how would you go about it in the code? 2: Have your character moving as per normal, and switch view when you read the end of the screen, so you kind a do it in spikes (I kind a like this idea). Thanks for all the help I'm receiving in here btw, it's awesome. Hopefully I can chip back some day |
Edgar Reynaldo
Major Reynaldo
May 2007
![]() |
Question #1 : 1typedef struct Button {
2 int x,y,w,h;
3 const char* text;
4};
5
6Button* MakeButton(int x , int y , int w , int h , const char* text) {
7 Button* b = (Button*)malloc(sizeof(Button));
8 if (!b) {return 0;}
9 b->x = x;
10 b->y = y;
11 b->w = w;
12 b->h = h;
13 b->text = strdup(text);
14 return b;
15}
16
17void DestroyButton(Button* b) {free(b->text);}
18
19int ButtonClicked(Button* b , int mousex , int mousey) {
20 // check if button contains mousex and mousey
21}
22
23void Display(Button* b , BITMAP* dest) {
24 // draw button text centered on the button
25}
Question #2 : My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
Tquila
Member #13,741
November 2011
|
Thanks bud, 1 question though. Where do I specify the range of what you draw? |
Edgar Reynaldo
Major Reynaldo
May 2007
![]() |
The range of what? Do you mean the area where you draw the text, or the button? That is based on the x,y,w,h members of the Button struct. My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
Tquila
Member #13,741
November 2011
|
Ah yeah, I can see that would seem cryptic. Got the button stuff (I think), but was thinking of what you draw on the screen. Where do I specify I want to draw the interval between x = 200 and x = 840 and y = 500 and y = 980. Where do I set what is visible on the screen? |
Edgar Reynaldo
Major Reynaldo
May 2007
![]() |
Make a simple camera object : 1typedef struct Rectangle {
2 int x,y,w,h,brx,bry;
3};
4
5Rectangle MakeRectangle(int x , int y , int w , int h) {
6 Rectangle r;
7 r.x = x;
8 r.y = y;
9 r.w = w;
10 r.h = h;
11 r.brx = x + w - 1;
12 r.bry = y + h - 1;
13 return r;
14}
15
16typedef struct Camera {
17 Rectangle viewdest;
18 Rectangle viewsrc;
19};
20
21void SetCameraPos(Camera* cam , int x , int y) {
22 if (x < cam->viewsrc.x) {x = cam->viewsrc.x;}
23 if (y < cam->viewsrc.y) {y = cam->viewsrc.y;}
24 if (x > cam->viewsrc.brx + 1 - cam->viewdest.w) {
25 x = cam->viewsrc.brx + 1 - cam->viewdest.w;
26 }
27 if (y > cam->viewsrc.bry + 1 - cam->viewdest.h) {
28 y = cam->viewsrc.bry + 1 - cam->viewdest.h);
29 }
30 cam->x = x;
31 cam->y = y;
32}
33
34void SetCameraDestination(Camera* cam , Rectangle dest) {
35 if (dest.w > cam->viewsrc.w) {dest.w = cam->viewsrc.w;}
36 if (dest.h > cam->viewsrc.h) {dest.h = cam->viewsrc.h;}
37 SetCameraPos(cam , dest.x , dest.y);
38}
39
40Camera MakeCamera(Rectangle dest , Rectangle src) {
41 Camera cam;
42 cam.viewsrc = src;
43 SetCameraDestination(&cam , dest);
44 return cam;
45}
And then to use it, you would call MakeCamera and SetCameraPos when necessary : Or to use your example : SetCameraPos(&cam , 200 , 500); // OR SetCameraDestination(&cam , Rectangle(200,500,640,480));
My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
Gabriel Campos
Member #12,034
June 2010
![]() |
Getting aboard this topic... Edgar said this function Buttom *MakeButtom(...) Why using Pointer Functions??
|
Audric
Member #907
January 2001
|
Gabriel Campos said:
Getting aboard this topic...Edgar said this function Button *MakeButton(...)
The space may be misleading, it's identical to Button* MakeButton(...) or (I think) (Button *) MakeButton(...). It's the same pattern as load_bitmap(). Very classic in C. |
DanielH
Member #934
January 2001
![]() |
Might want to fix your malloc and free functions Edgar. You malloc a button*, but free a char* (that wasn't malloc'ed). Pointers are useful. It's a matter of both style and necessity. Here is an example of 3 identical buttons 1const char *text = "Hello World!";
2
3// dynamic button
4Button* button1
5
6// static button
7Button button2;
8
9// static button
10Button button3 = { 10, 10, 200, 200, text };
11
12void makeTheButtons()
13{
14 button1 = MakeButton( 10, 10, 200, 200, text );
15 if ( !button1 )
16 {
17 //oops
18 return;
19 }
20
21 button2.x = 10;
22 button2.y = 10;
23 button2.w = 200;
24 button2.h = 200;
25 button2.text = text;
26}
27
28void freeMem()
29{
30 if ( button1 )
31 {
32 free( button1 );
33 }
34
35 // nothing to do for button2 or button3
36}
|
Edgar Reynaldo
Major Reynaldo
May 2007
![]() |
DanielH said: Might want to fix your malloc and free functions Edgar. You malloc a button*, but free a char* (that wasn't malloc'ed). The only thing I forgot was to free the Button* itself. The free(b->text); is to free the string that was created with strdup. Gabriel Campos said: Getting aboard this topic... Edgar said this functionButtom *MakeButtom(...) Can you help me to understand???Why using Pointer Functions?? He said he was using C, not C++, and a Button stores a newly allocated const char* string, hence the Create/Destroy functions. My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
Gabriel Campos
Member #12,034
June 2010
![]() |
So, to answer once and for all (to my sanity to), what is the diference to use a object pointer from a normal object? // whats the better? cButtom Buttom; // or this cButtom *Buttom;
|
Edgar Reynaldo
Major Reynaldo
May 2007
![]() |
An object lives on the stack and will be destroyed when it goes out of scope, when the function it is declared in exits. A pointer has to be created/allocated by you and lives on the heap. It won't be destroyed until you do it yourself. It depends on what you want to do with it, that's all. Pointers are used in C because objects often have dynamic memory that they allocate, and give you 'create' functions that save you from having to allocate it all manually. My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
Audric
Member #907
January 2001
|
Both forms have their uses... some_function_or_block(void) { cButtom Buttom; // some code that uses Buttom Buttom.DoStuff(); // end of scope } "behind the scenes", the above behaves like: some_function_or_block(void) { cButtom *Buttom; Buttom = new Buttom(); // <-- allocate in memory and call constructor // some code that uses Buttom Buttom->DoStuff(); // it's a shorthand for (*Buttom).DoStuff(); // end of scope free(Buttom); <-- call destructor then deallocate from memory } The memory place will be different (stack instead of heap), but it doesn't make much more difference. You can run out of stack more easily than out of heap, but it's still not a frequent problem. Only with recursive functions and/or classes that contain lots of static data, like large arrays. |
|