pmgl / microstudio

Free, open source game engine online
MIT License
931 stars 106 forks source link

Access to sprite colors from code. #82

Closed ConstantineRetroGamer closed 2 years ago

ConstantineRetroGamer commented 2 years ago

I've been thinking about this thing for a very long time: accessing the sprite palette from code. This technology was used on the NES console. Of course, then it was used to circumvent the memory limit. This is my version of how it might look in microScript:

  screen.drawSprite("cub",x,y,width,height)

  //sprites.cub.colors = [color1,color2]
  //sprites.cub.colors[color1] = "rgb(255,255,0)"
  //sprites.cub.colors[color2] = "rgb(255,0,0)"

  sprites.cub.colors[color1] = "rgb(0,0,255)"
  sprites.cub.colors[color2] = "rgb(0,255,85)"

"sprites.cube.colors" is a list of all colors for a "cub" sprite. Thus, you can use different coloring options of the same tile in different levels of the game. This opens up some interesting possibilities:

  1. Use less memory in your projects. I came across the fact that I could use the same tiles in different parts of the program by simply changing their palette. This would allow you to store fewer images in the project.
  2. The ability to make some parts of the sprite transparent. Maybe like this: "sprites.cube.colors[color1] = false"
  3. The ability to create an array containing a palette of 64 colors. Color all tiles using this palette. At the same time, tiles can be stored even in black and white. In some way, it will be possible to feel like a developer on the NES :-)
  4. You can create a flickering effect. I ran into such a problem when I wanted to make the boss in the game flicker. Or when I wanted to make a character in the game flicker when he raises a powerup (as in Super Mario Bros). Currently I am implementing flicker only like this:

    if invulnerability_time == true then
      if Player.display % 2 == 0 then Player.display = 1 else Player.display = 0 end
    end
    if invulnerability_time == false then Player.display = 1 end

It is difficult to achieve flickering of the character (changing the color alternately) using animation. Let's say we have a character with a bunch of animations for each action. Then for such a flicker, we will need to make separate animations, with the addition of frames with a different palette, through one. So it is very labor-intensive.

Perhaps this will open up some other opportunities that I do not know about. But I think it's worth paying attention. In addition to this, I would like to have a tool "replace a certain palette color with another" (in the sprite editor). When the image is large and has a complex pattern, the fill tool does not help (too difficult). In my opinion, this is the only reason why I might need to use a different editor. You can even reflect this with a code, but not change the color. In my opinion, implementing a list containing a palette of sprites will also solve this problem (although I technically don't know how it can be implemented). I think the palette is stored for the sprite anyway, but how do I get access through the code? :-)

pmgl commented 2 years ago

No plans to add this very specific feature, but I am closing this, considering that you can now achieve all this from your code. You have now access to your sprites and sprite frames and their pixel data. You can create a new Sprite, add multiple Image frames to it and set the pixel data of all of these image frames. See the API cheatsheet for more details about classes Sprite and Image.

https://github.com/pmgl/microstudio/wiki/en-API-cheatsheet#images