Jibble

Author Topic: PLUGIN CODE question [SOLVED]  (Read 249 times)

Dualnames

  • AGS Baker
  • Pretty Badass
    • Dualnames worked on a game that won an AGS Award!
    •  
    • Dualnames worked on a game that was nominated for an AGS Award!
PLUGIN CODE question [SOLVED]
« on: 12 Feb 2019, 02:55 »
I'm trying to code a PLUGIN function that grabs two sprites (sprite_a and sprite_b) parses the entirety of sprite_a and replaces the color of each pixel in sprite_b (with coordinates y = y from sprite a and x= x-Random(2) -2 from sprite_a), but that's not working, and i can't figure out why, I think I'm parsing the 2d array wrongfully.


Code: Adventure Game Studio
  1.  
  2. int x, y;  
  3.   BITMAP* src_a = engine->GetSpriteGraphic(sprite_a);
  4.   BITMAP* src_b = engine->GetSpriteGraphic(sprite_b);
  5.   unsigned short* pixel_a = *(unsigned short**)engine->GetRawBitmapSurface(src_a);
  6.   unsigned short* pixel_b = *(unsigned short**)engine->GetRawBitmapSurface(src_b);
  7.  
  8.   for (y = 0; y < screen_height; y++)
  9.   {
  10.     for (x = 0; x < screen_width; x++)
  11.     {
  12.           unsigned short colorfromB = pixel_b[y,x];
  13.           pixel_a[y,(x-(rand() % 2)-2)]=colorfromB;
  14.       pixel_a++;
  15.           pixel_b++;
  16.     }
  17.   }
  18.   engine->ReleaseBitmapSurface(src_a);
  19.   engine->ReleaseBitmapSurface(src_b);  
  20.  

Clarifying it's only doing half the screen, and it shouldn't, and i think the colors it picks are a bit wrong as well.


https://imgur.com/dMLLhUA
« Last Edit: 12 Feb 2019, 04:23 by Dualnames »
No more military army stuff. I'm alive and back.

tzachs

  • Parking Goat- games that goats like!
    • I can help with translating
    • tzachs worked on a game that was nominated for an AGS Award!
Re: PLUGIN CODE question
« Reply #1 on: 12 Feb 2019, 03:29 »
You're increasing x & y in the loop, but you're also increasing the pointers (pixel_a and pixel_b) inside the loop, so you're increasing twice instead of once.
Try removing pixel_a++ and pixel_b++.

Dualnames

  • AGS Baker
  • Pretty Badass
    • Dualnames worked on a game that won an AGS Award!
    •  
    • Dualnames worked on a game that was nominated for an AGS Award!
Re: PLUGIN CODE question
« Reply #2 on: 12 Feb 2019, 03:41 »
That reduces the effect into a line at the top of the screen. A single pixel 1 pixel height starting from 0 to halfway only one pixel height.


edit: Posting working code, thanks to qptain_nemo
Code: Adventure Game Studio
  1.  int x, y;
  2.  
  3.   BITMAP* src_a = engine->GetSpriteGraphic(sprite_a);
  4.  
  5.   BITMAP* src_b = engine->GetSpriteGraphic(sprite_b);
  6.  
  7.  
  8.   unsigned long** pixel_a = (unsigned long**)engine->GetRawBitmapSurface(src_a);
  9.   unsigned long** pixel_b = (unsigned long**)engine->GetRawBitmapSurface(src_b);
  10.  
  11.   for (y = 0; y < screen_height; y++)
  12.   {
  13.     for (x = 0; x < screen_width; x++)
  14.     {
  15.           unsigned long colorfromB = pixel_b[y][x];
  16.           pixel_a[y][(x-(rand() % 2)-2)]=colorfromB;     
  17.      // pixel_a++;//=2;
  18.          // pixel_b++;//=2;
  19.     }
  20.   }
  21.   engine->ReleaseBitmapSurface(src_a);
  22.   engine->ReleaseBitmapSurface(src_b);
  23.  // }
  24. }
  25.  
« Last Edit: 12 Feb 2019, 04:23 by Dualnames »
No more military army stuff. I'm alive and back.

tzachs

  • Parking Goat- games that goats like!
    • I can help with translating
    • tzachs worked on a game that was nominated for an AGS Award!
Re: PLUGIN CODE question [SOLVED]
« Reply #3 on: 12 Feb 2019, 04:31 »
Are you assuming here that screen_height and screen_width equal the bitmap's height and width?
Did you verify that?

You can do:
Code: C++
  1. int srcWidth, srcHeight;
  2. engine->GetBitmapDimensions(src_a, &srcWidth, &srcHeight, NULL);

And compare srcWidth/Height to screen_width/height (and also I'm assuming here that src_a and src_b are the same size, otherwise the code doesn't make a lot of sense).

EDIT: Ah, nvm, you solved it, cool.

Re: PLUGIN CODE question [SOLVED]
« Reply #4 on: 12 Feb 2019, 10:25 »
@Dualnames, I got that plugin code works for you now, but I have to warn against using "long" and suggest using "int" instead, or even include <stdint.h> and use "int32_t" type. There are two reasons for this:

1) when it comes to data format like image's it's better to stick to types that explicitly define their size.
2) "long" type is not portable: historically it always means 32-bit integer with Microsoft C++ compiler, but it is of varied size if you compile with gnu compilers (32-bit in 32-bit programs and 64-bit in 64-bit programs).

Dualnames

  • AGS Baker
  • Pretty Badass
    • Dualnames worked on a game that won an AGS Award!
    •  
    • Dualnames worked on a game that was nominated for an AGS Award!
Re: PLUGIN CODE question [SOLVED]
« Reply #5 on: 12 Feb 2019, 17:56 »
Alright, replaced all longs with int32, i've never coded much in c++ beyond like few lines, can you explain how to use int32_t, if that's not too much to ask.
Does it automatically convert the data types within stdint script? So i just do an include and keep using int and it will choose the appropriate one, unless explicitly defined by me?
No more military army stuff. I'm alive and back.

Re: PLUGIN CODE question [SOLVED]
« Reply #6 on: 12 Feb 2019, 20:25 »
Alright, replaced all longs with int32, i've never coded much in c++ beyond like few lines, can you explain how to use int32_t, if that's not too much to ask.
Does it automatically convert the data types within stdint script? So i just do an include and keep using int and it will choose the appropriate one, unless explicitly defined by me?

int32_t is simply a typedef (alias). stdint.h declares these aliases according to the system you compile under, but I think it just equals to "signed int" on most contemporary common platforms.
After you included <stdint.h> you may declare variables as "int32_t myint;" or "int32_t **pixel_a", same way as you use "int" and other types.
Some explanation why using these may be needed: https://stackoverflow.com/questions/14515874/difference-between-int32-int-int32-t-int8-and-int8-t

On the other hand if you think all this is too confusing you may just use "int" which should be okay on usual Windows/Linux/ etc systems you usually distribute games for. Just don't use "long" for the reasons I explained earlier.
« Last Edit: 12 Feb 2019, 20:30 by Crimson Wizard »