Mode7 - (Solved)

Started by Dualnames, Sun 05/06/2011 19:03:07

Previous topic - Next topic

Dualnames

I use an object graphic cause I felt it's the best way. I'm trying to replicate a mode7 effect similar to the one in final fantasy 3 in SNES at the intro. With the difference that it will be playable by keyboard (you know moving ahead).

How the code works:
-It gets the viewport of the scrolling background and sets it into a dynamic sprite. Then cuts that sprite in 12 pieces (sections). Resizes pieces accordingly by an individual rate for width and height(ratew and rateh) pastes the pieces together on a dynamic sprite and sets that sprite onto an object.
Worked on Strangeland, Primordia, Hob's Barrow, The Cat Lady, Mage's Initiation, Until I Have You, Downfall, Hunie Pop, and every game in the Wadjet Eye Games catalogue (porting)

Khris

The problem seems to be that ratew isn't a value independent of the number of sections. Ideally, a certain ratew value should produce the same transformation regardless of the number of sections.

Code: ags
parts[ad].Resize(parts[ad].Width-((sections-ad)*ratew), parts[ad].Height-((sections-ad)*rateh));


This part fails as soon as (section-ad)*ratew is greater than Width or (sections-ad)*rateh is greater than height.

The biggest value for sections-ad is right at the the start at the loop when ad is 0: sections

So in essence, this produces the error:
240/sections < sections*rateh
240 < rateh*sections²

With 12 sections, rateh is already limited to 0 and 1: 240 < 144*rateh.

Similar for the width:
320 < ratew*sections

With 12 sections, ratew mustn't be greater than 26. (26*12 = 312, 27*12 = 324)
If you increase the sections though, this limit shrinks rapidly.

The why is addressed, now how to solve this?

First of all, indent your code ;D
The } above fx.Release(); is the one from the while loop, and the one below is the one closing the function body, so object[0].Graphic=res.Graphic; ends up being outside the function. probably a C+P mistake.

To solve this, use floats for the rates and divide them by the number of sections before resizing the sprite.
Thus a specific rate produces a specific transformation and you can also adjust the rates much more precisely.

abstauber

Maybe you can also learn something useful from Easy3D.
That's  mode7 completely written in AGS Script:

http://www.adventuregamestudio.co.uk/yabb/index.php?topic=26130.0

Dualnames

#3
Thanks, Khris, now it works, I updated the code on the first post, cleaning it up a bit. I like not to indent my code, i feel it looks prettier that way. It's just a personal preference. And I fixed the bracket issue.

Quote from: abstauber on Mon 06/06/2011 10:07:49
Maybe you can also learn something useful from Easy3D.
That's  mode7 completely written in AGS Script.

I know, but I pretty much want to do this on my own, that's all. I'm a stubborn dick, what can I say?

EDIT: And here comes an issue i didn't guess. No wonder what, I can't get the image to update based on value of integer AX. integer d is the rate in which it should increase/decrease. Weirdly enough when i change the ax on the beginning(that means on the header) it changes the image, but not in-game. I tried deleting sprites and stuff, but no go.  :(
Worked on Strangeland, Primordia, Hob's Barrow, The Cat Lady, Mage's Initiation, Until I Have You, Downfall, Hunie Pop, and every game in the Wadjet Eye Games catalogue (porting)

Khris

Quote from: Dualnames on Mon 06/06/2011 14:10:13I like not to indent my code, i feel it looks prettier that way.

Sorry, but that's where I draw the line.

I don't indent my code because I think it looks prettier that way (it still does though), I do it so my code isn't an unreadable mess. Did you already forget about the brackets being messed up?

Good luck.

Dualnames

#5
Quote from: Khris on Tue 07/06/2011 08:03:19
Quote from: Dualnames on Mon 06/06/2011 14:10:13I like not to indent my code, i feel it looks prettier that way.

Sorry, but that's where I draw the line.

I don't indent my code because I think it looks prettier that way (it still does though), I do it so my code isn't an unreadable mess. Did you already forget about the brackets being messed up?

Good luck.

I totally fail to see the point here.
Worked on Strangeland, Primordia, Hob's Barrow, The Cat Lady, Mage's Initiation, Until I Have You, Downfall, Hunie Pop, and every game in the Wadjet Eye Games catalogue (porting)

Matti

You didn't indent a single line, you just rearranged the brackets ;)

Code: ags

int ax, ox,fxx=25, fyy=0, fww, sections=20, d=20, ad;
DynamicSprite*scale, res, parts[];
DrawingSurface*bg, fx;
float rw=5.0, rh=1.0;

function room_Load()
{
  bg=Room.GetDrawingSurfaceForBackground();
  object[0].Clickable=false;
  parts=new DynamicSprite[sections];
}

function room_RepExec()
{
  if (ax>Room.Height-240)
  {
    ax=Room.Height-240;
  }
  while (ad!=sections) 
  {
    int sze=(240/sections);  
    float ratew=rw/IntToFloat(sections);
    float rateh=rh/IntToFloat(sections);
    scale=DynamicSprite.CreateFromDrawingSurface(bg, 0, ax, 320, 240);
    parts[ad]=scale;
    parts[ad].Crop(0, (sze*ad), 320, sze);
    parts[ad].Resize(parts[ad].Width-((sections-ad)*FloatToInt(ratew)), parts[ad].Height-((sections-ad)*FloatToInt(rateh)));
    fx=res.GetDrawingSurface();
    if (ad>0)
    {
      fww=(parts[ad].Width-parts[ad-1].Width)/2;
    }
    fx.DrawImage(fxx-fww, fyy+parts[ad].Height-1, parts[ad].Graphic);
    fyy=fyy+parts[ad].Height-1;
    fxx=fxx-fww;
    ad++;
  }
  ad=0;
  fx.Release();
  object[0].Graphic=res.Graphic;
  object[0].SetPosition(0, 240);
}

function on_key_press(int keycode) 
{
  if (keycode==eKeyUpArrow) ax+=d;
  if (keycode==eKeyDownArrow) ax-=d;
}


I'm totally with Khris here, unindented code is way to messy to even bother with.

Dualnames

Quote from: Matti on Tue 07/06/2011 14:29:20
I'm totally with Khris here, unindented code is way to messy to even bother with.

I totally fail to see the point here.
Worked on Strangeland, Primordia, Hob's Barrow, The Cat Lady, Mage's Initiation, Until I Have You, Downfall, Hunie Pop, and every game in the Wadjet Eye Games catalogue (porting)

Khris

#8
EDIT:
Are you serious!?

You do see that indenting isn't just putting {}s in their own lines but actually INDENTING the lines, as in, move them to the right?

Seeing the point isn't the only thing you're failing at here I'm afraid.



Thanks for indenting the code Matti :)

Dualnames:
I hope you see my point now :)

When I test this,
Code: ags
    fx=res.GetDrawingSurface();

throws a null pointer error because res doesn't point to a DynamicSprite. That didn't happen before and isn't the problem with the code though so I won't focus on that.

One problem is that although you did turn the rates into floats, it's pointless if you use them like that.
You're setting rw to 5.0, then divide it by 20.0. So far so good.
Then you're turning the result (0.25) into an int though before multiplying it with (sections-ad); FloatToInt(0.25) is 0 though.

The first part should be:
  parts[ad].Width-FloatToInt(IntToFloat(sections-ad)*ratew, eRoundNearest)
Going from float back to int is always the very last step, otherwise rounding errors accumulate or distort the result.

Dualnames

Dear Khris,

   I believe that indentation, regardless of the fact that it is considered, by a very great percentage of people worldwide (and this community), as integral and user-friendly, when it comes to debugging and sharing the code, is the single most useless and annoying thing in the universe, next to kazoo and Friday by Rebecca Black.

  However, more annoying is the fact that you so bravely fight in favor of the indentation, failing to see the forest and looking at the trees. As much as i'd like anyone's help, it's not my personal fight to force anyone to help me out against HEARTLESS conditions, such as the lack of indentation.

I look forward to your answer,
James T. Kirk.
Worked on Strangeland, Primordia, Hob's Barrow, The Cat Lady, Mage's Initiation, Until I Have You, Downfall, Hunie Pop, and every game in the Wadjet Eye Games catalogue (porting)

abstauber

Look, I made a compressed version. It saves space and is err.. good to be shared ???

Code: ags

int ax, ox,fxx=25, fyy=0, fww, sections=20, d=20, ad;DynamicSprite*scale, res, parts[];
DrawingSurface*bg, fx;float rw=5.0, rh=1.0;function room_Load(){  bg=Room.GetDrawingSurfaceForBackground();  
object[0].Clickable=false;  parts=new DynamicSprite[sections];}function room_RepExec(){  
if (ax>Room.Height-240)  {ax=Room.Height-240;  }  while (ad!=sections)   {    
int sze=(240/sections); float ratew=rw/IntToFloat(sections);    
float rateh=rh/IntToFloat(sections);    scale=DynamicSprite.CreateFromDrawingSurface(bg, 0, ax, 320, 240);    
parts[ad]=scale;    parts[ad].Crop(0, (sze*ad), 320, sze);    
parts[ad].Resize(parts[ad].Width-(sections-ad)*FloatToInt(ratew)), parts[ad].Height-((sections-ad)*FloatToInt(rateh)));    
fx=res.GetDrawingSurface(); if (ad>0){fww=(parts[ad].Width-parts[ad-1].Width)/2;}
fx.DrawImage(fxx-fww, fyy+parts[ad].Height-1, parts[ad].Graphic);    
fyy=fyy+parts[ad].Height-1;fxx=fxx-fww;ad++;}  ad=0;  
fx.Release();  object[0].Graphic=res.Graphic;  object[0].SetPosition(0, 240);}function on_key_press(int keycode) 
{if (keycode==eKeyUpArrow) ax+=d;if (keycode==eKeyDownArrow) ax-=d;}


I'm sorry that this didn't help at all, but
Quotewhen it comes to debugging and sharing the code, is the single most useless and annoying thing in the universe,
equals
QuoteThe world's a disc

Dualnames

Worked on Strangeland, Primordia, Hob's Barrow, The Cat Lady, Mage's Initiation, Until I Have You, Downfall, Hunie Pop, and every game in the Wadjet Eye Games catalogue (porting)

Matti

"single most useless and annoying thing"  ???

It's quite the opposite, cause the one reading the code has to either indent it himself or go through it and count/compare all the brackets which is quite annoying. Also, not indenting the code generally makes it quite error prone which has been the case here (in the tech forums) countless times. I understand that you're used to not indenting code and may have trouble changing that, but I don't at all get why you don't see its great advantage.

In your case the indentation was easy, otherwise I wouldn't have bothered..

Btw, you know that instead of

Code: ags
if (ad>0)
{
fww=(parts[ad].Width-parts[ad-1].Width)/2;
}


you can just write

Code: ags
if (ad>0) fww=(parts[ad].Width-parts[ad-1].Width)/2;


Makes things even shorter and more readable  ;)

Wyz

It's not just indenting, also putting spaces around operators, putting brackets on new lines and choosing a programming style and then sticking to it. I simple refuse to read code that isn't properly indented, for some people I make exceptions, then I will first indent their code and then read it. But seriously, the compiler doesn't give a rats arse about indentation (not talking about python) or comments, but humans do.

itsforthesamereasonwhywedonttypelikethisbecauseitmakesitallnotveryreadableeventhoughitmightsafesomespaceey

But you are special Dualsie  :-*
Life is like an adventure without the pixel hunts.

Snarky

If someone actively refuses to indent their code properly, I will simply refuse to read it, or help them with any problem they might have. So good luck with your problem, hope you figure it out for yourself.

Khris

Dualnames:
I couldn't care less about what you think of the usefulness of indentation.

You want help with your code, and after I politely mentioned that you should indent your code - not just because I think it's useful but because you actually screwed up the placement of the brackets which proper indentation helps prevent - you keep being an ass about it even though other people chimed in how it's common courtesy not to dump messy code here when asking for help.

The irony is that you call indentation the "single most useless and annoying thing" when that's precisely what best describes the attitude you're displaying here.

So again, good luck.

abstauber

#16
Sorry, I can't get it to work. I created a sample project and pasted Matti's version of your code in it. But it crashes as this line, as soon as I hit a key:
Code: ags
scale=DynamicSprite.CreateFromDrawingSurface(bg, 0,ax,  320, 240);


This is because ax can have a negative value by pressing down and the drawing surface immediately gets out of bounds.

It's a bummer that you've edited your posts, so that I can't access a working version.


Dualnames

Quote from: Snarky on Tue 07/06/2011 16:04:11
If someone actively refuses to indent their code properly, I will simply refuse to read it, or help them with any problem they might have. So good luck with your problem, hope you figure it out for yourself.

Let me quote myself.

QuoteAs much as i'd like anyone's help, it's not my personal fight to force anyone to help me out against HEARTLESS conditions, such as the lack of indentation.
Worked on Strangeland, Primordia, Hob's Barrow, The Cat Lady, Mage's Initiation, Until I Have You, Downfall, Hunie Pop, and every game in the Wadjet Eye Games catalogue (porting)

Ryan Timothy B

#18
Unless you're some kind of genius Rainman who can identify exactly what's in between each of your ninja braces. Or you're relying heavily on Ctrl+B to point out where the closing brace is, or just simply counting every brace until you find where you want to insert your code. I couldn't program like that. It's unbelievably time consuming and, as everyone keeps saying, Very prone to errors. It has nothing to do with 'looking pretty', it's simply just to make it organized and easier on you and anyone else who looks at your code.

I have a hard enough time finding the closing brace on my indented lines when there's tons of lines of code. I'd hate to imagine programming in your style. Seriously. We're not trying to convert you to the darkside or something.

When I first started programming back in grade 9, I never used indentation either. I refused to use it. I thought the teacher was trying to teach us some stupid method to waste time. But a couple years later, once I started using it, I found my code unbelievably clear and easy to read and now laugh at people like you (who used to be like me) who think it's a waste of time.

But no, seriously. Stick with your style if it really makes you happy spending all that time counting your braces and putting the code between the wrong ones and having to cut and paste several times. :P

ddq

Dualnames, please fuck the fuck off. Proper indentation is crucial for writing clear and readable code. Your brain is broked.

SMF spam blocked by CleanTalk