Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Help with level editor user interface

This thread is locked; no one can reply to it. rss feed Print
 1   2   3 
Help with level editor user interface
Felix-The-Ghost
Member #9,729
April 2008
avatar

Right, okay I'll do that. See I had to reset it several times yesterday. I really hope it's not still running from then O_O edit: thanks, Edgar. You hit the nail on the head, I had like 4-5 of them still running. I'm surprised my computer did not die a horrible death. Some of them looked like the processes weren't using much to run though... Anyhow it's working as I intended it to now :) hey you know whats pretty cool when you put two bangs !! its like they're not there

-----------------------------------------------------------------------------------

Okay I need help again.
I got the above to work although I edited commented out some of it's code so I can work on getting a working scrollbar. I am trying to take advantage of a ratio:
full bitmap size / view size
should equal
scroll area / scrollbar

I thought I had the math part right but it's drawing slightly off. It looks fine at first, but when scrolled all the way down the scollbar overlaps the button :P

I have the function at the very bottom; draw_scrollbar.
Any help would be appreciated.
And sorry if you think the code is messy. I usually do tests like these before cleaning up the code.

I think it's calculating the X wrong, but I think the stretched width is correct.

==========================
<--- The ghost with the most!
---------------------------
[Website] [Youtube]

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

I haven't looked at the code, but don't forget to scale the offset for the position of the top of the scrollbar as well. That could be why it overextends. Another consideration is that you need to track the offset of the mouse click on the scrollbar and record it when you start to drag the scrollbar so it doesn't move too far in either direction.

Felix-The-Ghost
Member #9,729
April 2008
avatar

Heh. I can tell you didn't look at the code :P
Thanks for helping though. See I haven't tried to get the scrollbar to function yet...I was just getting it to show the view position accurately. The only way to scroll right now is the buttons. Also the view is vertical scrolling, but the scrollbar is horizontal scrolling. Weird I know, but I like the idea. If you do look at the code, the function is on the very bottom.

But why would I need to scale the offset? I'm using stretched blitting so it just has the first x,y and the width and height, which are relative to the first x and y. So if I got the x wrong...the whole thing's wrong.

==========================
<--- The ghost with the most!
---------------------------
[Website] [Youtube]

LennyLen
Member #5,313
December 2004
avatar

I took a look at the code, but your program is getting so big now (especially for a single file project) that trying to figure out exactly what you're doing is difficult. It doesn't help that some of the variable names you're using aren't very descriptive.

If you can isolate the scrollbar code into a new smaller project, then it would be easier to help you, and when the problem is sorted, you can fix it in the main project.

This might also be a good time to break your code down into smaller modules, before it gets too big to work with easily.

Felix-The-Ghost
Member #9,729
April 2008
avatar

Do you mean I should permanently separate it or just for the sake of easy aid? What sort of things to people usually separate? I was thinking about getting the bitmap loading into a new file but I didn't really know how I'd do that. I can try to make a new program with the scrollbar code if that's what you want. Thanks. Oh and which variable names aren't descriptive? Some of the vague ones should have comments next to their declarations if that helps. I don't remember having any ridiculous ones though...the worst is that some of them are similar, such as Scrollbar->w and Scrollbar_Width. The first is the length of the bitmap file, the second is the width of the stretched scrollbar.

==========================
<--- The ghost with the most!
---------------------------
[Website] [Youtube]

LennyLen
Member #5,313
December 2004
avatar

Quote:

I can try to make a new program with the scrollbar code if that's what you want.

That's what I meant, yes.

Quote:

What sort of things to people usually separate?

It varies from project to project, but for yours, I'd initially seperate it into the 'main' module, which contains your main() function plus your variable definitions and your initialization routines; a 'drawing' module; and a 'logic' module. You can then break these down further if necessary.

edit:

Quote:

Oh and which variable names aren't descriptive?

TestClipT and TestBuffer were the two I was thinking of.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Quote:

What sort of things to people usually separate?

I separate things that are easily recognized as discrete objects or ideas. In effect, things like widgets, sets of data, data manipulation, generic i/o handlers, etc...

When you asked me about why the scrollbar offset should be scaled, I was talking about the offset of the drawing position of the scrollbar handle in relation to the scrollbar area.

Felix-The-Ghost
Member #9,729
April 2008
avatar

I don't know how I'd do that...
Would I use the same ratio?

Also would there ever be a need to group related variables? I sort of did that with this code but they are still all in the same file. Should I make the functions separate like put them in another .CPP? If I include the file in the DevProject it'll arrange it the necessary way automatically?

Oh and LennyLen, TestBuffer is the name of the loaded bitmap that is clipped. Look in the part where it loads bitmaps, probably one of the last ones. The TestClipT stands for TestClipTop; it represents the top of the clipping area of the TestBuffer(which is drawn at the top of the view area). I increase this by one (with limits) when the scrollbutton is clicked, and blit with this variable and a fixed height to produce a clipping effect. Once I get the scrollbar working I'll actually remove TestBuffer and related and just hard-code the drawing of tiles onto a new one, so when there are updates the scrollbar remains relative with the ratio.

I guess I'll get started with a new one since I already had gotten decent dragging support for the bar and a splash screen. I'll strip all the other functions off though.

edit: attached the stripped down version. I removed everything except the splash screen, because it's sexy :)

drag the scrollbar as far as it goes, then click the scrolldown button and you'll see it goes a little further.

==========================
<--- The ghost with the most!
---------------------------
[Website] [Youtube]

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Quote:

Would I use the same ratio?

Yes. All you need to do is find the ratios for :
a) The total length of the object being viewed (including the non-visible portion) to the total length of the scrollbar area (not including buttons).

b) The length of the visible portion of the object being viewed to the total length of the viewable object.

The ratio of (a) is your stretch factor in that dimension. The ratio of (b) times the length of the scrollbar area gives you the length of the scrollbar handle.

The scrollbar handle's offset divided by the ratio of (b) gives you the actual size of the viewing object's drawing offset.

It's best to just draw out the design on paper first, and then when you know what you're doing, put it into code. That method helps me finish things anyway.

Quote:

Also would there ever be a need to group related variables? I sort of did that with this code but they are still all in the same file. Should I make the functions separate like put them in another .CPP?

Well, it would probably help you to make a separate module for your map data and methods, a map class that knows which tiles can be in it and holds an index to a tile list for each position in the map. Another module could be used for your editor. A third module could be for a tileset class, that's used by your map class and your editor class, etc...

Felix-The-Ghost
Member #9,729
April 2008
avatar

Okay I get it, very distinct things get grouped together.
like...

drawing everything
editor mouse logic
map area mouse logic
saving/loading
reset/clear all

?

Also I'll try using the ratio...I thought I tried it before but it didn't work...maybe I used the wrong one. I sketch math based things on paper too. Or MS Paint.

edit: okay this is not working out so well...

1////// CHECK MOUSE ON SCROLLBAR //////////
2 ScrollRatio = (TestBuffer->h / ScrollArea); //ratio of the entire bitmap's height to the scrollbar area's length
3 ViewRatio = (TestBuffer->h / TestClipHeight); //ratio of the entire bitmap's height to the visible portion
4
5 if (my <= (Scrollbar_Y_Offset+Scrollbar->h) && my >= Scrollbar_Y_Offset && mx <= (Scrollbar_X_Pos+Scrollbar_Width) && mx >= Scrollbar_X_Pos && (mb & 1) && !(old_mb & 1) && Scroll_Active == false) //if left mouse is clicked over the scrollbar
6 {
7 Scroll_Active = true;
8 Scrollbar_Clicked_X = (mx-Scrollbar_X_Pos); //relative X position
9 old_mb = mb; //initialize
10 }
11
12 if (Scroll_Active == true)
13 {
14 if (mb == old_mb) //if the button hasn't been let go
15 {
16 Scrollbar_X_Pos = (mx-Scrollbar_Clicked_X);
17
18 if (Scrollbar_X_Pos < Scrollbar_X_Offset)
19 {
20 Scrollbar_X_Pos = Scrollbar_X_Offset; //don't expand past the left button
21 }
22 if ((Scrollbar_X_Pos + Scrollbar_Width) > (Scrollbar_X_Offset + ScrollArea))
23 {
24 Scrollbar_X_Pos = (Scrollbar_X_Offset + ScrollArea - Scrollbar_Width); //don't expand past the right button
25 }
26 TestClipT = ((Scrollbar_X_Pos - Scrollbar_X_Offset) * ScrollRatio);
27 }
28 else
29 {
30 Scroll_Active = false;
31 }
32 }
33}

void draw_scrollbar() //draws a scrollbar of [in]correct scale
{
    ScrollRatio = (TestBuffer->h / ScrollArea); //ratio of the entire bitmap's height to the scrollbar area's length
    ViewRatio = (TestBuffer->h / TestClipHeight); //ratio of the entire bitmap's height to the visible portion
    Scrollbar_X_Pos = (TestClipT/ViewRatio)+(Scrollbar_X_Offset/ScrollRatio);
    Scrollbar_Width = (TestClipHeight/ViewRatio);

    stretch_sprite(Buffer, Scrollbar, Scrollbar_X_Pos, Scrollbar_Y_Offset, Scrollbar_Width, Scrollbar->h); //having difficulties here
     
}

I used to use just one ratio; big to small. I thought (TestBuffer->h / ScrollArea) was the same as (TestClipHeight / Scrollbar_Width)
gosh why does the code tag stretch the page on my computer...?

==========================
<--- The ghost with the most!
---------------------------
[Website] [Youtube]

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Felix-The-Ghost
Member #9,729
April 2008
avatar

Would that fix it? So you mean just put
double(int x) * double x ?
I think I had already set the ratios as doubles, but you mean with everything else, right?

==========================
<--- The ghost with the most!
---------------------------
[Website] [Youtube]

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Quote:

Would that fix it?

It will get rid of the inaccuracy caused by clipping of the fractional part. Is that the only thing wrong? I don't know, but I can try looking over things more tomorrow.

Store the ratios as a double, use temporary doubles or casts to prevent the integer division.

//
int a = 12 , b = 5;
double atob = double(a)/double(b);// using double constructors
double atob2 = (double)a/(double)b;// using casts to double type

int also_a = int(atob*double(b));// may not actually get exactly 'a' back
                                 // due to floating point inaccuracy
//

Please try to be consistent in your formatting schemes, it makes it a lot easier to read code if you do. (Don't mix tab indentation with space indentation, always indent when there is a new block {})

Felix-The-Ghost
Member #9,729
April 2008
avatar

?
I use DevCpp. When I indent with the tab button it adds a lot of spaces, or at least it seems so because I often highlight them individually and delete some to make the indents even. Where did I not indent at a new block? I didn't see it, although that new code was just the old one after deleted a lot of stuff. I guess I'll start cleaning up my code before I post it here.
What are atob and atob2?
And when I tried casting doubles it made the scrollbar over-expand in both directions.

==========================
<--- The ghost with the most!
---------------------------
[Website] [Youtube]

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Quote:

I use DevCpp. When I indent with the tab button it adds a lot of spaces, or at least it seems so because I often highlight them individually and delete some to make the indents even.

DevCpp may have a setting to control the number of spaces that a tab press will indent. CodeBlocks certainly does, but if there are tabs in the source code then you have to look at arrows every place there's a tab so I just replace them all with 2 or 3 spaces depending on what I'm coding for.

Quote:

Where did I not indent at a new block?

Things like this (from your last code posting) :

            if (Scrollbar_X_Pos < Scrollbar_X_Offset)
            {
            Scrollbar_X_Pos = Scrollbar_X_Offset; //don't expand past the left button               
            }

Quote:

I guess I'll start cleaning up my code before I post it here.

If you get in the habit of always doing your formatting consistently, you'll never have to 'clean it up'.

Quote:

What are atob and atob2?

The ratio of a to b and a second method to find the same ratio. Just an example.

Quote:

And when I tried casting doubles it made the scrollbar over-expand in both directions.

Okay, so let's see what your code is now.

Felix-The-Ghost
Member #9,729
April 2008
avatar

Yeah, I format my main code consistently, but I mean for stuff like this I usually just throw the required/important things together with minimum editing because I don't plan on reusing it. That's where I forgot to reindent after deleting some other blocks that weren't needed(yeah I copy+pasted if I was typing it it would have looked better :). So does atob stand for something? I'll get the new code on here. I'll fix it up first too. :P

==========================
<--- The ghost with the most!
---------------------------
[Website] [Youtube]

someone972
Member #7,719
August 2006
avatar

It goes too far left because you scale the offset, this should remain the same.

Scrollbar_X_Pos = (double(TestClipT)/ViewRatio)+(double(Scrollbar_X_Offset)/ScrollRatio);
//should be 
Scrollbar_X_Pos = (double(TestClipT)/ViewRatio)+double(Scrollbar_X_Offset);

The ViewRatio should be calulated using ScrollArea.

ViewRatio = (double(TestBuffer->h) / double(TestClipHeight));
//should be
ViewRatio = (double(TestBuffer->h) / double(ScrollArea));

This works correctly for me. It should stay where it is supposed to now.

______________________________________
As long as it remains classified how long it took me to make I'll be deemed a computer game genius. - William Labbett
Theory is when you know something, but it doesn't work. Practice is when something works, but you don't know why. Programmers combine theory and practice: Nothing works and they don't know why. -Unknown
I have recklessly set in motion a chain of events with the potential to so-drastically change the path of my life that I can only find it to be beautifully frightening.

Felix-The-Ghost
Member #9,729
April 2008
avatar

Gosh you edited your post... :-/
I was all trying to using the width like you said. :P
But...Edgar said to scale the offset...
wait let me try this.
If you calcuated ViewRatio using ScrollArea, then what did you calculate ScrollRatio with?

==========================
<--- The ghost with the most!
---------------------------
[Website] [Youtube]

someone972
Member #7,719
August 2006
avatar

Ya, sorry about that. I was rapidly changing my post as I figured things out, at the end removing all the unnecessary pieces hoping you hadn't seen yet.:-X
EDIT: Hmm, I changed a lot, let me see what the original was and then I tell you what I was sopposed to change.

It appears as though you only need to change ViewRatio to Scroll Ratio in the width area and change the X position as stated above.

ScrollRatio = (double(TestBuffer->h) / double(ScrollArea)); //original
ViewRatio = (double(TestBuffer->h) / double(TestClipHeight)); //original
Scrollbar_X_Pos = (double(TestClipT)/ViewRatio)+double(Scrollbar_X_Offset); //changed this
Scrollbar_Width = (double(TestClipHeight)/ScrollRatio); //and this

EDIT2: sorry again, I'm changing this too.

______________________________________
As long as it remains classified how long it took me to make I'll be deemed a computer game genius. - William Labbett
Theory is when you know something, but it doesn't work. Practice is when something works, but you don't know why. Programmers combine theory and practice: Nothing works and they don't know why. -Unknown
I have recklessly set in motion a chain of events with the potential to so-drastically change the path of my life that I can only find it to be beautifully frightening.

Felix-The-Ghost
Member #9,729
April 2008
avatar

Can you scroll all the way to the right and tell me if the is a little gap there? I edited mine and there is a little pixel gap. It scrolls and clicks the buttons the same now though. I wonder if I misunderstood Edgar. So you're saying that ScrollRatio and ViewRatio are actually the same now? That's what I was saying earlier...

Quote:

I used to use just one ratio; big to small. I thought (TestBuffer->h / ScrollArea) was the same as (TestClipHeight / Scrollbar_Width)

also is this page stretched to you?

Can you post again instead of editing please for clarity?

==========================
<--- The ghost with the most!
---------------------------
[Website] [Youtube]

someone972
Member #7,719
August 2006
avatar

Okay this one should work this time:

Scrollbar_X_Pos = (double(TestClipT)/ScrollRatio)+double(Scrollbar_X_Offset);
Scrollbar_Width = (double(TestClipHeight)/ScrollRatio);

The Ratio's are the same as in the original file you posted. The pixel gap is there because ScrollArea is 184 and should be 185 I think.

EDIT: This way seems to continue to work properly when the scroll area is increased.

______________________________________
As long as it remains classified how long it took me to make I'll be deemed a computer game genius. - William Labbett
Theory is when you know something, but it doesn't work. Practice is when something works, but you don't know why. Programmers combine theory and practice: Nothing works and they don't know why. -Unknown
I have recklessly set in motion a chain of events with the potential to so-drastically change the path of my life that I can only find it to be beautifully frightening.

Felix-The-Ghost
Member #9,729
April 2008
avatar

gosh so many edits...
anyways I edited the original file. You can drag the scrollbar correctly...but something you haven't checked is when I calculate TestClipT inside of mouse_logic(the part when you drag the scollbar, not the buttons). Because it is calculated wrong I can click the scroll down button even when it is at the maximum allowed position.

==========================
<--- The ghost with the most!
---------------------------
[Website] [Youtube]

someone972
Member #7,719
August 2006
avatar

I guess I don't understand the problem now. Perhaps all my edits got something changed that wasn't supposed to be. Check my binary that's attached and see if it works as desired. If it does I can give you the right source.

______________________________________
As long as it remains classified how long it took me to make I'll be deemed a computer game genius. - William Labbett
Theory is when you know something, but it doesn't work. Practice is when something works, but you don't know why. Programmers combine theory and practice: Nothing works and they don't know why. -Unknown
I have recklessly set in motion a chain of events with the potential to so-drastically change the path of my life that I can only find it to be beautifully frightening.

Felix-The-Ghost
Member #9,729
April 2008
avatar

Huh. Yeah that works as I intended. Perhaps you changed something else I didn't?
Also I edited the original source where it looks like you edited the new one. I'll have to see what's different.

==========================
<--- The ghost with the most!
---------------------------
[Website] [Youtube]

someone972
Member #7,719
August 2006
avatar

Ok, sorry it took so long to reply, I've been watching COPS. The only function I changed is the draw_scrollbar, here is the one I am using:

void draw_scrollbar()
{
    ScrollRatio = (double(TestBuffer->h) / double(ScrollArea));
    ViewRatio = (double(TestBuffer->h) / double(TestClipHeight));
    Scrollbar_X_Pos = (double(TestClipT)/ScrollRatio)+double(Scrollbar_X_Offset);
    Scrollbar_Width = (double(TestClipHeight)/ScrollRatio);

    stretch_sprite(Buffer, Scrollbar, Scrollbar_X_Pos, Scrollbar_Y_Offset, Scrollbar_Width, Scrollbar->h);
};

I have also attached the entire source file if changing that doesn't work.

______________________________________
As long as it remains classified how long it took me to make I'll be deemed a computer game genius. - William Labbett
Theory is when you know something, but it doesn't work. Practice is when something works, but you don't know why. Programmers combine theory and practice: Nothing works and they don't know why. -Unknown
I have recklessly set in motion a chain of events with the potential to so-drastically change the path of my life that I can only find it to be beautifully frightening.

 1   2   3 


Go to: