Author Topic: Best way to: Put an image background  (Read 1323 times)

KeiDash

  • Newbie
  • *
  • Posts: 16
  • Karma: +1/-0
  • Software developer
    • View Profile
    • amelian.eu
Best way to: Put an image background
« on: June 24, 2019, 03:46:05 pm »
Hi everyone.

In order not to create new entries, I take advantage of this same space to ask the question.

See the question here please



I'm doing some test on JoEngine. I've started with printing some text on screen with jo_printf function based on gamepad keys pressed and would like to know the best way to do this, I'm explain it.

I've two functions, function A and function B. Each one, has a validation that if the key A was pressed, continue doing something. The A method call to B method when A key was pressed.

Code: [Select]
void A()
{
   do
   {
      //do something here
   } while(!jo_is_pad1_key_pressed(JO_KEY_A))

   B();
}

void B()
{
   do
   {
      //do something here too
   } while(!jo_is_pad1_key_pressed(JO_KEY_A))
}

In this case, if in A function the A button was pressed, then call at B function.

The problem? The call from A to B it's so fast that the B function ends because it detected that the A button was pressed. It's something like the A buttom continued pressed in both cases or the time elapsed was very short that function B detected that A buttom was pressed to.

Exists some method or technique that wait between one call to other?


In other side, I've errors continuously making the compilation, but the output doesn't say something like Error in file.c X line X.. throw messages like:

Code: [Select]
sh-coff-gcc.exe: main.o: No such file or directory

I'm making the compilation on Windows Command Line and in the Windows Power Shell. I solved it deleting the *.o files and compiling again, but I don't understand where is the problem.

Thanks at all.

Regards,
« Last Edit: July 01, 2019, 10:27:48 am by KeiDash »

ponut64

  • Full Member
  • ***
  • Posts: 219
  • Karma: +22/-1
    • View Profile
1. Use compile.bat and edit compile.bat to ensure it ends with PAUSE. At which point you don't need any command line, just double-click it and it will halt so you can read what happened.
If you're still missing main.o, check the makefile in the project directory. it must list your source files in the SRCS line.

2.

Both function A and B halts the program if you never press A. THE ENTIRE PROGRAM.

Instead:

Code: [Select]
void stuff_for_A(void){

static bool lastTouch = false;

 if(jo_is_pad1_key_pressed(JO_KEY_A)){
 jo_printf(lineX, lineY, "stuff%i", data);
 lastTouch = true;
 } else if(lastTouch == true){
 jo_printf(lineX, lineY, "                                         ");
 lastTouch = false;
}

}
What does this do:
When you press the A button, print text.
When you release the A button, on the next frame, clears that text area off the screen.

If you want to insert a wait, add a timer. Don't try and halt by process, that slows down the entire program.
And of course, I'm not talking about some abstraction of a timer. You make an integer and add to it every frame. When it reaches the number you want, run the associated function and set it back to zero.
« Last Edit: June 25, 2019, 05:02:19 am by ponut64 »

KeiDash

  • Newbie
  • *
  • Posts: 16
  • Karma: +1/-0
  • Software developer
    • View Profile
    • amelian.eu
1. Use compile.bat and edit compile.bat to ensure it ends with PAUSE. At which point you don't need any command line, just double-click it and it will halt so you can read what happened.
If you're still missing main.o, check the makefile in the project directory. it must list your source files in the SRCS line.

2.

Both function A and B halts the program if you never press A. THE ENTIRE PROGRAM.

Instead:

Code: [Select]
void stuff_for_A(void){

static bool lastTouch = false;

 if(jo_is_pad1_key_pressed(JO_KEY_A)){
 jo_printf(lineX, lineY, "stuff%i", data);
 lastTouch = true;
 } else if(lastTouch == true){
 jo_printf(lineX, lineY, "                                         ");
 lastTouch = false;
}

}
What does this do:
When you press the A button, print text.
When you release the A button, on the next frame, clears that text area off the screen.

If you want to insert a wait, add a timer. Don't try and halt by process, that slows down the entire program.
And of course, I'm not talking about some abstraction of a timer. You make an integer and add to it every frame. When it reaches the number you want, run the associated function and set it back to zero.

Thanks for your reply.

As I see, I'm not on the wrong track. I'm doing something similar to what you explain, with a variable that "prevent" the next action, but, I wans't using a static var. As you say, it's better in this case.

I thought that this "practice" maybe no was the best way to do that.

For the compilation issue, I've checked my makefile and it's right. It has all files that I need in the SRCS line, included main.c. The curious case is that I included a new line in compile.bat. I wrote DEL *.o as second line before throw the compilation and now works always right.

I don't know if this happening only at me or exists more users that has the same issue.

KeiDash

  • Newbie
  • *
  • Posts: 16
  • Karma: +1/-0
  • Software developer
    • View Profile
    • amelian.eu
Re: Best way to: Put an image background
« Reply #3 on: July 01, 2019, 10:25:59 am »
I use the same message to avoid creating another post.

I'm looking the Samples in the JoEngine to improve my knowledge. I'm trying to put 2 different background images in "2 screens" (one method call other and each one has it own bg).

I'm using the jo_tga_loader function like this:
Code: [Select]
   //SCREEN 1
   imgBackground.data = JO_NULL;
   jo_tga_loader(&imgBackground, "BG", "IMG1.TGA", JO_COLOR_Transparent);
   jo_set_background_sprite(&imgBackground, 0, 0);
   jo_free_img(&imgBackground);

   //SCREEN 2
   imgBackground.data = JO_NULL;
   jo_tga_loader(&imgBackground, "BG", "IMG2.TGA", JO_COLOR_Transparent);
   jo_set_background_sprite(&imgBackground, 0, 0);
   jo_free_img(&imgBackground);

The first screen (the menu) load the IMG1.TGA file. When the A button it's pressed, clear the screen and the background and then loads the IMG2.TGA.

If in the screen 2, the B button it's pressed, clear the screen and the background too and come back to the screen 1 and loads again the IMG1.TGA file.

Up to this point there are no problems. The problem comes now, if I repeat the process. If I press the A button again to change SCREEN 1 to 2, the engine throws an error saying that the file "IM___.TGA" file not found.



1. In the error, the file name not is complete, the complete file name is "IMG2.TGA", whats happening here?
2. To view a possible function anotations, I went to read the documentation of jo_tga_loader, but this method doesn't exists on the original documentation, instead exists the function jo_tga_loader_from_stream

What's going on?
This is the best way to load an image background?
It's a bad idea load a TGA file in the background (for the performance)?
If jo_tga_loader_from_stream is the only one method, how I can convert the image background to binary to use this method?
Exists any other option?

Thanks all.

PD: I'm using Yabause or mednafen to do it, but each emulator works different.
« Last Edit: July 01, 2019, 10:38:58 am by KeiDash »

ponut64

  • Full Member
  • ***
  • Posts: 219
  • Karma: +22/-1
    • View Profile
Re: Best way to: Put an image background
« Reply #4 on: July 01, 2019, 04:31:26 pm »
It's possible that one or another of jo engine's memory pointers are going out of range when you repeat this much.

It may not be in the documentation, but it is definitely in the source code.

Upon review, every call of jo_tga_loader will malloc.
It is true that jo_free_img is supposed to clear this. But, it is important you ensure that this function never repeats on a pointer unless that pointer is allocated.

There's also the issue of runtime performance.
The file system expects a system halt. You could try putting a while loop on tga loader and jo_free calls. This isn't for a logical reason, instead it is for the reason that the SH2 will cache instructions up to a conditional jump, could be causing problems.

Another thing is that upon review, jo_tga_loader should work fine loading different files (as long as the first file is the maximum image size you'll use) to the same image data region without being free'd. img->data is never treated with an incremental pointer; none is stored except in for loops. jo_free_img destroys the pointer and points img->data to NULL. That can cause errors if it's ever used out of order.

Why would it ever be used out of order?
VDP2 and Slave SH2 don't halt where Master SH2 halts.
Also, potentially compiler errors.

I've also had success clearing up file system errors by disassociating file names. IMG1 and IMG2 could be too similar and subtle memory corruptions that "just happen" with Jo engine make the code confused. It's possible that IM  .TGA is expressing that the text "G1" and/or "G2" were corrupted into illegal text codes. I suggest you specify "IMD1" and "IBG2" or something like that.

I will say that whatever Yabause is doing, ignore it. It's most likely wrong.
Trust Mednafen.

KeiDash

  • Newbie
  • *
  • Posts: 16
  • Karma: +1/-0
  • Software developer
    • View Profile
    • amelian.eu
Re: Best way to: Put an image background
« Reply #5 on: July 03, 2019, 09:05:34 am »
First of all, thank you so mutch about your explanation Mr.Ponut. Feels good when in the community the users help each other.

I think that I understod your explanation. I revice my code again and I found a mistake, something that you are talking about. I'm doing wrong the logic where the backgrounds it's loader because in each cycle of the process,

I'm erasing and load again the background in each cycle and I'm sure that this provoque (as you say) an out of memory exception. I change my code and now the bg it's loaded one time only using the same jo_img var, using the same pointer because the bg's has the same weight and height.

On the other hand, I've changed the TGA image file for a converted image to .c with the JoMapEditor. In this case, the images was loaded more fast than physical files (logically..) on the screen and now I don't have any errors or out of memory exceptions. If someone are interested:

Code: [Select]
void SetBackgroundByScreen()
{

    jo_img bgImage;
    bgImage.data = JO_NULL;
    bgImage.width = 320;
    bgImage.height = 240;

    switch (currentScreen)
    {
        case SCREEN_MAIN:
            if(enableBackground != 0) { break; }

            enableBackground = 1;

            bgImage.data = (unsigned short *)BG1;

            jo_clear_background(JO_COLOR_Black);
            jo_set_background_sprite(&bgImage, 0, 0);
            jo_free_img(&bgImage);

            break;
        case SCREEN_OUTP:
            if(enableBackground != 0) { break; }

            enableBackground = 1;

            bgImage.data = (unsigned short *)BG2;

            jo_clear_background(JO_COLOR_Black);
            jo_set_background_sprite(&bgImage, 0, 0);
            jo_free_img(&bgImage);

            break;
        default:
            enableBackground = 0;
            bgImage.data = JO_NULL;
           
            break;
    }

}



 

Sitemap 1 2 3 4 5 6 7 8 9 10