Code a game competition Nov-Jan 2018 - Minimum £500 prize fund

Started by Qube, November 15, 2017, 04:44:30

Previous topic - Next topic

RemiD

Here is a code to count the number of colors in an image : (blitz3d)

;count the colors in an image
Graphics(320,240,32,2)

SeedRnd(MilliSecs())

Global PixAlpha% = 0
Global PixRed% = 0
Global PixGreen% = 0
Global PixBlue% = 0

Global InImage = LoadImage("wip-render-320w200h-scaled2x-201712192217.png")

Global ColorsCount%
Dim Color_R%(16)
Dim Color_G%(16)
Dim Color_B%(16)

;count how many colors there are in the image
SetBuffer(ImageBuffer(InImage))
LockBuffer(ImageBuffer(InImage))
For PX% = 0 To ImageWidth(InImage)-1 Step 1
For PY% = 0 To ImageHeight(InImage)-1 Step 1
  ReadPixFast(PX,PY)
  ColorAlreadyExists% = False
  For OI% = 1 To ColorsCount Step 1
   If( PixRed = Color_R(OI) And PixGreen = Color_G(OI) And PixBlue = Color_B(OI) )
    ColorAlreadyExists = True
    Goto LineEndCheckIfColorAlreadyExists
   EndIf
  Next
  .LineEndCheckIfColorAlreadyExists
  If( ColorAlreadyExists = False )
   ;create a new color
   ColorsCount = ColorsCount + 1
   I% = ColorsCount
   Color_R(I) = PixRed : Color_G(I) = PixGreen : Color_B(I) = PixBlue
  EndIf
Next
Next
UnlockBuffer(ImageBuffer(InImage))

SetBuffer(BackBuffer()) : Cls()
Locate(0,0) : Color(255,255,255)
Print("ColorsCount = "+ColorsCount)
For I% = 1 To ColorsCount Step 1
Print(Color_R(I)+","+Color_G(I)+","+Color_B(I))
Next
Flip(True)

WaitKey()

End()

Function WritePix(PX%,PY%,R%,G%,B%,A%=255)
HexARGB = RGBAToHexARGB(R,G,B,A)
WritePixel(PX,PY,HexARGB)
End Function

Function ReadPix(PX%,PY%)
HexARGB% = ReadPixel(PX,PY)
HexARGBToRGBA(HexARGB%)
End Function

Function WritePixFast(PX%,PY%,R%,G%,B%,A%=255)
HexARGB = RGBAToHexARGB(R,G,B,A)
WritePixelFast(PX,PY,HexARGB)
End Function

Function ReadPixFast(PX%,PY%)
HexARGB% = ReadPixelFast(PX,PY)
HexARGBToRGBA(HexARGB%)
End Function

Function RGBAToHexARGB%(R%,G%,B%,A%)
HexARGB% = A Shl(24) + R Shl(16) + G Shl(8) + B Shl(0)
Return HexARGB
End Function

Function HexARGBToRGBA(HexARGB%)
PixAlpha = HexARGB Shr(24) And 255
PixRed = HexARGB Shr(16) And 255
PixGreen = HexARGB Shr(8) And 255
PixBlue = HexARGB Shl(0) And 255
End Function


apparently if the (bilinear) filtering of textures is desactivated, the scaling of the 320x200 rendered image, copied to a texture stretched on a quad which fills a bigger window (640x400 or 960x600) does not add others colors than those of the initial render. So this works well here. (i have posted a code example in reply #327 for those of you who are curious on how to achieve that)

col

@RemiD
Following post #327 you seem concerned about the values of the UVs?
The UVs are texture coordinates only, and range from 0 to 1 in the x,y axis of the texture. For eg U value of 0.0 is the left edge of the texture and 1.0 will be the right edge of the texture regardless of where the vertex is in the 2d/3d world. This means you can easily stretch and squash a texture just by moving the vertices to different positions as the texture will move with the vertices.

For eg if youre setting up the top left vertex of a quad then for d3d/blitz3d you use uv of 0.0, 0.0, the top right will be 1.0, 0.0, borrom left will be 0.0, 1.0 and bottom right will be 1.0, 1.0. As those values are in texture space ( texture coords ) the same values are used no matter where the vertices are actually positioned - the texture will stretch accordingly.
Foe opengl/vulkan the texture is flipped vertically so you just invert the V values.

If you have access to shaders through a blitz3d d3d9 lib(?) you can use a vertex shader to position vertices easily using what are called 'normalized device coordinates'. GaborD showed using a different method to get these values using the 0.0 to 1.0 range of the UVs ( NDCs rang -1 to +1 from the edges of the output window ). Shaders may seem difficult at first but they are so so worth the effort of learning. To learn properly you should really at least have a firm underatanding of the gpu pipeline. Its honestly not difficult but does require some good old fashioned studying.
https://github.com/davecamp

"When you observe the world through social media, you lose your faith in it."

BasicBoy

I appreciate the deadline extension (thanks Qube), although I do sympathize with the views of those who prefer that the original be stuck to.

I'm only just now getting into my stride with my entry!  (I just wish I had devised a better game concept to begin with, but I'm kind of stuck with it now...)


BasicBoy.
--

RemiD

Quote
The UVs are texture coordinates only, and range from 0 to 1 in the x,y axis of the texture. For eg U value of 0.0 is the left edge of the texture and 1.0 will be the right edge of the texture regardless of where the vertex is in the 2d/3d world. This means you can easily stretch and squash a texture just by moving the vertices to different positions as the texture will move with the vertices.
@col>>you are talking to me ? you are sure ? read my code example in post #327 and see how this explanation is not calibrated at all ;)

(and again i use Blitz3d, so no custom shaders !)

RemiD

I have coded a system where the user chooses the scale 1x, 2x, 3x, of the window and then the scene can be rendered either in 320x200 and then scaled 2x or 3x, using a texture stretched on a quad mesh which fills the whole window, or to directly render the chosen resolution (not allowed in the competition, but this was just by curiosity to see the difference)

And indeed, even only scaled 2x, a "normal" render looks much better (which is to be expected)
see :
320x200 scaled 2x (the window is 640x400)


640x400 normal (the window is 640x400)


Not sure if all kinds of scenes can look good in a scaled render...

Anyway !

GaborD

Quote from: col on December 20, 2017, 18:26:27
Shaders may seem difficult at first but they are so so worth the effort of learning.

Totally agree to that. They open up so many possibilities.
I would even say shaders may look hard at first, but they are actually quite simple if you stick to basic shader setups (which are quite honestly all you need until you start doing really crazy things) and think of it in digestable chunks.



GaborD

Quote from: RemiD on December 20, 2017, 21:20:41

Not sure if all kinds of scenes can look good in a scaled render...

Took me a long time to decide on a style and a palette for this reason.
Then again, you could embrace the blockiness as a part of the style. :D
Or you could supersample and then convert to the palette afterwards for some anti-aliasing that sticks to the 16 colors. Depends on the palette if it makes sense to do it though.


col

@RemiD
Ooops, sorry... I'm so used to getting textures at the actual size that I specify that I forgot about that power of 2 stuff :)
https://github.com/davecamp

"When you observe the world through social media, you lose your faith in it."

iWasAdam

@ RemiD
Pic 1 320x200. scaled to 640x400 - legal, no problem
pic 2 640x400. NOT LEGAL> if this is what you are using your game WILL fail

Derron

@ Adam

Quote from: RemiD[..] or to directly render the chosen resolution (not allowed in the competition, but this was just by curiosity to see the difference)
Think Remi is aware of this.

bye
Ron

iWasAdam

thanks derron  ;D

Todays develop is all about debugging. So I will be extensively play testing and trying to work out WTF went wrong!

RemiD

yes yes i am aware that the resolution of the game must be 320x200, this was just to demonstrate how ugly a scaled render is compared to a normal render (for the same window resolution)

iWasAdam


RemiD

All made manually ?

Personally this is the problem i have when making a game : if there is some randomness in it, so that there is something to explore/discover and some ways to be surprised (even for the creator of the game), i am motivated to do it, but if i have to build maps manually and i already know what's in them, what will happen, i have no motivation to do it !

iWasAdam

yep. all manually created and tweaked
here's level 1 retro:


and level 2 retro:


I'de swear in level 2 it looks like you can run right across the bottom and keep going...