Questions about DynamicSprite

Started by KingMick, Tue 11/04/2006 11:08:12

Previous topic - Next topic

KingMick

So I am trying to get the hang of this whole DynamicSprite thingy, and I'm left with several questions here.

First of all, I am daunted by the help-file's insistance that I delete the DynamicSprite as soon as I am "done using it".  One of the things I want to do in my game is create an area where the player can actually use their own custom picture in the game--specifically, there is a desk in the game with a picture frame on it, and that frame depicts the picture "MyPicture.jpg" or something like that.  I want the pictureframe object to load up MyPicture.jpg when the program starts and then keep that graphic for the remainder of the game.  If I use a DynamicSprite to import the picture, then tell the object to change its graphic to the DynamicSprite's graphic, should I then delete the DynamicSprite or not?  If I delete it, will it screw up the pictureframe object?  If I don't delete it, will it mess up the game's memory somehow?

Secondly, I want to include functionality in the game where the player can press a button (say F3, or Enter, or something) and the game will take a screenshot (just as it would do when saving) and EXPORT that screenshot to a file outside the game (Screen1.jpg or something).  Furthermore I need the game to be able to recognize if there is already a file by the desired name so that it will use a different name to avoid overwriting--i.e. if Screen1.jpg already exists, the game saves your screenshot as Screen2.jpg, or if that exists it saves it as Screen3.jpg, etc.  I see and understand DynamicSprite's screenshot function but what I can't figure out is (a) how to export the screenshot as an image file and (b) how to do the override-avoiding file-detection.

Radiant

Quote from: KingMick on Tue 11/04/2006 11:08:12
If I use a DynamicSprite to import the picture, then tell the object to change its graphic to the DynamicSprite's graphic, should I then delete the DynamicSprite or not?  If I delete it, will it screw up the pictureframe object?  If I don't delete it, will it mess up the game's memory somehow?
No, yes, and no. Deleting it when you're done using it, in this case, means deleting it when your game shuts down.

Quote
Secondly, I want to include functionality in the game where the player can press a button (say F3, or Enter, or something) and the game will take a screenshot (just as it would do when saving)
There's an easier way of doing that... use the SaveScreenshot() function, which in the default configuration is called when you press F12.

Quotehow to do the override-avoiding file-detection.
Basically, use File.Open to open screen0.jpg for reading. If it succeeds, close it and open the next one. Repeat until you fail to open one for reading. That's the lowest-numbered not-yet-existent file.

Gilbert

#2
Well this is the screenshot saving code taken from my not-yet-released game (quickly changed to be V2.71+ compatible):
Code: ags

Ã,  Ã,  if (keycode==434) {Ã,  // F12
Ã,  Ã,  Ã,  String tmpstr;
Ã,  Ã,  Ã,  File* sshandle;
Ã,  Ã,  Ã,  while (sscounter<1000){Ã,  Ã,  Ã,  Ã,  
Ã,  Ã,  Ã,  Ã,  sscounter++;
Ã,  Ã,  Ã,  Ã,  tmpstr = String.Format("game%03d.pcx",sscounter);
Ã,  Ã,  Ã,  Ã,  sshandle = File.Open(tmpstr, eFileRead);
Ã,  Ã,  Ã,  Ã,  if (sshandle == null) {
Ã,  Ã,  Ã,  Ã,  Ã,  SaveScreenShot(tmpstr);
Ã,  Ã,  Ã,  Ã,  Ã,  return;
Ã,  Ã,  Ã,  Ã,  Ã,  }
Ã,  Ã,  Ã,  Ã,  sshandle.Close();
Ã,  Ã,  Ã,  Ã,  }Ã,  Ã,  Ã,  
Ã,  Ã,  Ã,  }


Where sscounter is an int variable declared on top of the global script:
Code: ags
int sscounter=0;


Hope this will help.

SSH

I've sometimes found that this kind of code sometimes saves a screenshot for every game cycle that F12 is held which can mean hundreds. You may want to put some code in to prevent that, too. And GIlbert: you refer to tmpstr and tmpstr2... are you sure that code works?
12

Gilbert

Quote from: SSH on Tue 11/04/2006 11:45:02
I've sometimes found that this kind of code sometimes saves a screenshot for every game cycle that F12 is held which can mean hundreds.
No, it won't since it's supposed to be in on_key_press() which will be executed only once when a key is being pressed.

Quote
You may want to put some code in to prevent that, too. And GIlbert: you refer to tmpstr and tmpstr2... are you sure that code works?
Hehe, good point, the code was tested to work, the only problem was it was originally written for V2.6SP2 (which I AM using, not even OO yet) and was quickly modified to the recent versions (so this version was not tested), I'll fix the String name, hope it would work that way. :=

KingMick

Gilbot--looked over your code and it looks shorter and more efficient than the way I was going to handle it.  Do you mind if I just copy/paste what you've got there?

Thanks all for your help w/ these questions!

BTW what formats can it save screenshots as?  Is pcx the only option?  Is it the best option in terms of quality-to-size ratio?

Gilbert

No, I wouldn't mind, but tell us if it works or not, as I mentioned before that it's upgraded codes from old version of AGS, and these codes wasn't tested yet. :)

KingMick

#7
Here I am, back to do just that.

It needs modification.Ã,  First of all, I had to move "String tmpstr" to the script header and change "String" to "string".Ã,  Second of all, the String.Format thing does not work in the newest version of AGS.Ã,  The way to do it now is apparently like this:

Ã,  Ã,  Ã,  Ã, StrFormat(tmpstr, "scrn%03d.pcx",sscounter);

I found that in the help file and used it, and it worked.

After that there is only one problem--SSH was correct, more screenshots were saved than were supposed to be saved.Ã,  I pressed the F12 button twice and ended up with 6 screenshots.Ã,  My guess is that the on_key_press is getting called more than once--i.e., if you hold the button down it will keep calling it, so depending on how fast your computer is, the longer the button is down the more screenshots are saved.

So, I'll have to come up with some code to prevent it from running more than once.Ã,  If anyone has any ideas on how to do this, post it here.Ã,  Meanwhile I'm going to try to figure it out myself.

Overall structure of your code worked great, though, Gilbot.

EDIT: It's not related to how long the key is pressed, it's more than that.Ã,  After more testing I've found that the program continues taking screenshots until there is some kind of new input--a mouse-click works to stop it, and I assume another keypress would as well.Ã,  I tried having "keycode" set to 0 right before the return command, that changed nothing (I sort of thought it wouldn't, but I had to try).Ã,  I tried putting a Display command right before the return as well, thinking that since Display normally pauses the game until you click, this would solve the problem.Ã,  That didn't work either--the display box actually clears itself and the process repeats until you click the mouse.Ã,  I think what we need is for the program to simulate a mouse-click or keypress immediately after taking the screenshot, but I have no idea how to do this.  Anyone?

Gilbert

Well, seems that you're not using the newest version of AGS then (you're probably using V2.7 where the new String type is not introduced yet, the currest newest stable release is V2.71), since for new version.

In that case, the code should be (aghain not tested):
Code: ags

    if (keycode==434) {  // F12
      string tmpstr;
      while (sscounter<1000){        
        sscounter++;
        StrFormat(tmpstr,"game%03d.pcx",sscounter);
        sshandle = File.Open(tmpstr, eFileRead);
        if (sshandle == null) {
          SaveScreenShot(tmpstr);
          return;
          }
         sshandle.Close();
        }      
      }


Quote from: KingMick on Wed 19/04/2006 11:42:35
After that there is only one problem--SSH was correct, more screenshots were saved than were supposed to be saved.Ã,  I pressed the F12 button twice and ended up with 6 screenshots.Ã,  My guess is that the on_key_press is getting called more than once--i.e., if you hold the button down it will keep calling it, so depending on how fast your computer is, the longer the button is down the more screenshots are saved.

Ah yes, just tried this, I think it's probably because of the key repeating rate set for computers, so if you keep it pressed the action would still be repeated, though it wouldn't if you tap the key. I may think of some way against it later.

KingMick

You're right, I just checked and my version is 2.70 not 2.71

Regarding the repeating keypress issue--read the EDIT section in my above post.  It doesn't matter how long you hold it down.  The program will continually take screenshots even after you release the button until you give the program new input--i.e., a mouse click or a key press.  I have actually only tested the mouse-click but I assume another key press would also do it.

The only way I can think of to solve this problem is to have the program simulate a mouse-click immediately after taking the screenshot.  However, I cannot figure out how to do this. Right now unless the player himself stops the process by doing something interactive with the program, it will continually take more and more screenshots (even after the screenshot key is released).

GarageGothic

#10
There's no reason to simulate anything, just add a conditional that is reset whenever the button ISN'T PRESSED. So add a variable to the globalscript:

Code: ags
bool takingscreenshot;


Let's continue with a few modifications to Gilbot's code:

Code: ags
 if ((keycode==434) && (takingscreenshot == false)){  // F12
      string tmpstr;
      while (sscounter<1000){       
        sscounter++;
        StrFormat(tmpstr,"game%03d.pcx",sscounter);
        sshandle = File.Open(tmpstr, eFileRead);
        if (sshandle == null) {
          SaveScreenShot(tmpstr);
          takingscreenshot = true;
          return;
          }
         sshandle.Close();
        }     
      }


Now, add this to repeatedly_execute:

Code: ags

	if (takingscreenshot == true) {
	  if (IsKeyPressed(434) == false) takingscreenshot = false;	  
	  }


This should make sure that you can't take another screenshot until you let go of F12 and then press it again.

KingMick

LOL.  GarageGoth, based on my previous logic for why Gilbot's code wasn't working, I totally expected yours to not work either.  After all, if whatever buffer or block of memory that stores the key input is not cleared upon releasing it (as I thought the problem was), then your code would not have worked since it relies on the assumption that that block of memory or whatever IS cleared if the key is no longer pressed.  So I tested it out, believing I'd be posting here that it didn't work and explaining why it didn't work.

It worked.

So I'm still not sure what the problem is, but whatever it was, you solved it.  Thanks again, and thanks to Gilbot for the original code!


Gilbert

Actually it's quite odd, if the key was repeated automatically, even after you released the key, I can't think of the cause at the moment.

KingMick

Yeah, me neither--especially now, since the only explanation I could come up with was blown out of the water by the fact that Garage's solution worked.  It's a very strange bug, isn't it?  Probably something that ought to be brought to the attention of Chris Jones, if he's still the one who programs this thing, or whoever has taken his place if he's not.

SMF spam blocked by CleanTalk