Suggestions for improving some lite-c standard functions

Posted By: Alan

Suggestions for improving some lite-c standard functions - 08/24/09 14:21

Hi there,

I've been working with panels during the past few weeks a lot. As we all know, a panel is defined like this:

Code:
PANEL* mypan =
{
  pos_x = 160;
  pos_y = 120;
  [...]
}



Okay. In a screen resolution of 320x240px that would be the center of the screen. Now let's say we switch the screen resolution to 1024x768px. The absolute position of the panel on the screen would change and be somewhere on the upper left. So, I tried to improve the code and did something like this:

Code:
PANEL* mypan =
{
  pos_x = screen_size.x / 2;
  pos_y = screen_size.y / 2;
  [...]
}



In theory, this makes sure that the panel is always at the center of the screen. However, it cannot be compiled since lite-c NEEDS a numeric value in the "pos_x" parameter. A var parameter is NOT accepted, which would be very useful. You might say that a var parameter there would cost a lot of CPU power because whenever the var changes, the position of the panel has to change as well - I claim that it would be enough if the panel reads its position out of the variable at the time of its creation and then maintains that position, no matter what the var's content might be later on, until the "pos_x" or "pos_y" parameter is changed manually.


Another example of where a var parameter is impossible: the WINDOW command for panels.

Code:
window(x, y, dx, dy, bmap, varx, vary);



bmap, varx and vary can be given a variable or pointer. dx and dy, however, expect a numeric value. However, it would be so *VERY* useful if dx and dy could be given variables as parameters. Why? Let us assume we have an energy bar. Its bitmap displays a color gradient from red (on the left side) to green (on the right end). Currently, we can only "move" the window within the bitmap. If we do so, the red and green end and all the colors in between would change their position, making the entire color gradient senseless. However, if we were able to manipulate the actual SIZE of the window on the bitmap, the colors always maintain their position (if the player has high HitPoints, the right end of the bar is green, the lower they get the more red the right end of the bar gets as the size of the window is decreasing as well).

Do you know what I mean? Basically what I would suggest is: free the parameters! Expect the user to enter numeric values OR variables for EVERY parameter a function/struct has. I don't know whether this is possible or not, but after all, this is just a suggestion ^^'

Greets,

Alan
Posted By: Dark_samurai

Re: Suggestions for improving some lite-c standard functions - 08/24/09 16:26

Quote:
A var parameter is NOT accepted, which would be very useful.


Is already in the forecast.

Edit: It seems like they removed it, but I'm sure that this is a planned feature, event if it's not high priority.
Posted By: jcl

Re: Suggestions for improving some lite-c standard functions - 08/24/09 16:39

For changing a variable in a programming language, such as a panel position, you must use a function. This is common to all languages and explained in the first workshops of the tutorial.
Posted By: Alan

Re: Suggestions for improving some lite-c standard functions - 08/24/09 16:52

@JCL: Changing the position of a panel *after* it has been created is easy. I would like to define its position using a variable at the time the panel is CREATED. The main purpose is the one I described above - changing screen resolutions can not cause the panels to change their position if the pos_x and pos_y are defined "screen relative".
And what about the window-element and its dx and dy parameters? I can't imagine a function which is capable of changing these two values...
Posted By: Enduriel

Re: Suggestions for improving some lite-c standard functions - 08/24/09 17:00

why don't you write inside your "change resolution" function to use pan_setpos function? that function takes a vector, the Z component can be 0.

and also you can always access the panels pos_x with panel.pos_x = 50; as an example. You could always write that inside your "change resolution" function aswell.
Posted By: jcl

Re: Suggestions for improving some lite-c standard functions - 08/24/09 17:10

You can change all panel elements - look in the manual under "Panel functions". Predefining panel movement already in the definition offers no advantage and makes no sense to me.

Posted By: EvilSOB

Re: Suggestions for improving some lite-c standard functions - 08/24/09 19:10

Hey Alan, you could always do it my way.
NEVER define a panel...

Just create a "panel_action" function that creates the panel with pan_create,
and arranges its position, size and all its buttons etc.
Then this panel_action STAYS RUNNING, watching for a resolution change,
and then re-arranges itself if there was a resolution change.

Works for me...
Posted By: Alan

Re: Suggestions for improving some lite-c standard functions - 08/25/09 11:50

@EvilSOB: Hm, sounds good... I did it the way Enduriel explained, but your solution for this problem sounds good, too!


@JCL: Defining the pos_x and pos_y coordinates in a panel definition has nothing to do with movement. If the variable given changes after the panel was defined, the panel does not have to change its position as well. The only purpose for a variable to be in a definition is flexibility - the flexibility to define screen-relative (and therefore resolution-independent) panel-positions. Of course this can be done in a function after the definition, but it would be cool if one could define things like

Code:
var my_screen_size_x = 1024;
var my_screen_size_y = 768;

PANEL* my_test_pan =
{
pos_x = my_screen_size_x / 2 + 30;
pos_y = my_screen_size_y / 2 + 50;
[...]
}


void main()
{
screen_size.x = my_screen_size_x;
screen_size.y = my_screen_size_y;

[...]

my_screen_size_x *= 2;        //the panel maintains its absolute position here,
my_screen_size_y *= 2;        //although the resolution is changed!
screen_size.x = my_screen_size_x;
screen_size.y = my_screen_size_y;

}



No matter what I enter for "my_screen_size_x" and "my_screen_size_y", the panel would maintain its relative position on the screen.
Of course, as mentioned, it can be done afterwards by a function. But a definition like the one I did above would make that function unneccessary.
Posted By: JibbSmart

Re: Suggestions for improving some lite-c standard functions - 08/25/09 12:39

Why not use a #define? Since the panels get defined right at the very beginning, the only variables that would be relevant are global variables' default values, so instead of doing that you could just use a #define (since they just get replaced at compile time anyway).

Jibb
Posted By: Alan

Re: Suggestions for improving some lite-c standard functions - 08/25/09 15:29

@JulzMighty: Could you give me an example? What you say sounds like the solution I've been looking for...
Posted By: jcl

Re: Suggestions for improving some lite-c standard functions - 08/25/09 15:50

A #define would indeed make sense, but does not work here either because engine objects don't accept #defines for their default values. But #defines for engine objects are indeed on our implementation list.

At the moment though, a function is the best way to set your panel position to the screen resolution.
Posted By: Alan

Re: Suggestions for improving some lite-c standard functions - 08/25/09 18:03

Well then - I'm looking forward to it!
And by the way... what about the dx and dy values of windows (Panel element)? Currently, they cannot be changed after their creation, even with a function (please correct me if I'm wrong). It would be great if that was possible. Please refer to the start post for an example.
Posted By: JibbSmart

Re: Suggestions for improving some lite-c standard functions - 08/26/09 00:19

@jcl: Wow, I had no idea about that. I just assumed all #defines were replaced with their appropriate values just before it gets compiled.

@Alan: In C (and Lite-C) you can put in your code (usually near the top with your #includes) a #define.
eg:
Code:
#define SCALESIZE 15
...
...
action scaledEnt() {
// scale all dimensions of the entity by a pre-determined constant
   my.scale_x = my.scale_y = my.scale_z = SCALESIZE;
}

This is common practice to remove "magic numbers" (that is, constants in your code that may or may not need to change later on in production); this makes the code more readable (someone reading my code doesn't know why I'm scaling this entity by 15, but if they see it scaled by a #define they can usually see what purpose it is serving). It also means if you need to change that constant, and it is used a lot in your code, you can just change it in the #define.

It doesn't have to be a number, either. It's often used for skills:
Code:
#define HEALTH skill1
#define VITALITY skill1
...
...
my.HEALTH = 30;
my.VITALITY = 30; // redundant, since VITALITY and HEALTH both become "skill1"

You can have two different defines for the same skill or value (as I've just done), but you have to be sure one entity won't need them both to be different things (here HEALTH and VITALITY mean the same thing, but if an entity needs HEALTH and VITALITY to be different, one of them should be #defined to a different skill instead; this is just to show its flexibility).

Does anyone care to explain why it won't work for engine objects? That sounds weird to me, although I obviously don't know exactly how the compiler works.

Jibb
Posted By: Alan

Re: Suggestions for improving some lite-c standard functions - 08/26/09 08:35

@JulzMighty: Ah, I understand what you mean now. I wonder whether or not it is possible to do calculations within defines as well, like this:

Code:
#define ss_x 1024     //ss_x = screen_size_x
#define ss_y 768      //ss_y = screen_size_y
#define panpos_x (ss_x / 2)
#define panpos_y (ss_y / 2)

PANEL* my_pan = 
{
   pos_x = panpos_x;
   pos_y = panpos_y;
}



I can't test that out myself because according to JCL defines can not be parameters for panel positions, but would the defines for panpos_x and panpos_y in my example above work that way?
Posted By: JibbSmart

Re: Suggestions for improving some lite-c standard functions - 08/26/09 08:43

The defines just stick in what's there before compilation, so it won't perform the calculation for you, it'll still end up being "pos_x = 1024 / 2;", I'm pretty sure.

Jibb
Posted By: EvilSOB

Re: Suggestions for improving some lite-c standard functions - 08/26/09 10:33

JutzMighty is correct.
As a MASSIVE over-simplification, the "define" is just a "text replacement tool".
Posted By: 3Dski

Re: Suggestions for improving some lite-c standard functions - 09/07/09 16:37

Is panel_create slow and a lot of overhead to place in an action? Why wouldn't you create the panel, then only change pos_x and pos_y in the action?
© 2024 lite-C Forums