shaders_palette_switch.bmx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. SuperStrict
  2. Framework Ray.Lib
  3. ?Not opengles
  4. Const GLSL_VERSION:Int = 330
  5. ?opengles
  6. Const GLSL_VERSION:Int = 100
  7. ?
  8. Const MAX_PALETTES:Int = 3
  9. Const COLORS_PER_PALETTE:Int = 8
  10. Const VALUES_PER_COLOR:Int = 3
  11. Local palettes:Int[][] = [ ..
  12. [ .. ' 3-BIT RGB
  13. 0, 0, 0, ..
  14. 255, 0, 0, ..
  15. 0, 255, 0, ..
  16. 0, 0, 255, ..
  17. 0, 255, 255, ..
  18. 255, 0, 255, ..
  19. 255, 255, 0, ..
  20. 255, 255, 255 ..
  21. ], ..
  22. [ .. ' AMMO-8 (GameBoy-like)
  23. 4, 12, 6, ..
  24. 17, 35, 24, ..
  25. 30, 58, 41, ..
  26. 48, 93, 66, ..
  27. 77, 128, 97, ..
  28. 137, 162, 87, ..
  29. 190, 220, 127, ..
  30. 238, 255, 204 ..
  31. ],..
  32. [ ..' RKBV (2-strip film)
  33. 21, 25, 26, ..
  34. 138, 76, 88, ..
  35. 217, 98, 117, ..
  36. 230, 184, 193, ..
  37. 69, 107, 115, ..
  38. 75, 151, 166, ..
  39. 165, 189, 194, ..
  40. 255, 245, 247 ..
  41. ] ..
  42. ]
  43. Local paletteText:String[] = [..
  44. "3-BIT RGB", ..
  45. "AMMO-8 (GameBoy-like)",..
  46. "RKBV (2-strip film)"..
  47. ]
  48. ' Initialization
  49. '--------------------------------------------------------------------------------------
  50. Const screenWidth:Int = 800
  51. Const screenHeight:Int = 450
  52. InitWindow(screenWidth, screenHeight, "raylib [shaders] example - color palette switch")
  53. ' Load shader to be used on some parts drawing
  54. ' NOTE 1: Using GLSL 330 shader version, on OpenGL ES 2.0 use GLSL 100 shader version
  55. ' NOTE 2: Defining 0 (NULL) for vertex shader forces usage of internal default vertex shader
  56. Local shader:RShader = LoadShader(0, "../../lib.mod/raylib/examples/shaders/resources/shaders/glsl" + GLSL_VERSION + "/palette_switch.fs")
  57. ' Get variable (uniform) location on the shader to connect with the program
  58. ' NOTE: If uniform variable could not be found in the shader, function returns -1
  59. Local paletteLoc:Int = GetShaderLocation(shader, "palette")
  60. Local currentPalette:Int = 0
  61. Local lineHeight:Int = screenHeight/COLORS_PER_PALETTE
  62. SetTargetFPS(60) ' Set our game to run at 60 frames-per-second
  63. '--------------------------------------------------------------------------------------
  64. ' Main game loop
  65. While Not WindowShouldClose() ' Detect window close button or ESC key
  66. ' Update
  67. '----------------------------------------------------------------------------------
  68. If IsKeyPressed(KEY_RIGHT) Then
  69. currentPalette :+ 1
  70. Else If IsKeyPressed(KEY_LEFT) Then
  71. currentPalette :- 1
  72. End If
  73. If currentPalette >= MAX_PALETTES Then
  74. currentPalette = 0
  75. Else If currentPalette < 0 Then
  76. currentPalette = MAX_PALETTES - 1
  77. End If
  78. ' Send new value to the shader to be used on drawing.
  79. ' NOTE: We are sending RGB triplets w/o the alpha channel
  80. SetShaderValueV(shader, paletteLoc, palettes[currentPalette], SHADER_UNIFORM_IVEC3, COLORS_PER_PALETTE)
  81. '----------------------------------------------------------------------------------
  82. ' Draw
  83. '----------------------------------------------------------------------------------
  84. BeginDrawing()
  85. ClearBackground(RAYWHITE)
  86. BeginShaderMode(shader)
  87. For Local i:Int = 0 Until COLORS_PER_PALETTE
  88. ' Draw horizontal screen-wide rectangles with increasing "palette index"
  89. ' The used palette index is encoded in the RGB components of the pixel
  90. DrawRectangle(0, lineHeight*i, GetScreenWidth(), lineHeight, New RColor(i, i, i, 255))
  91. Next
  92. EndShaderMode()
  93. DrawText("< >", 10, 10, 30, DARKBLUE)
  94. DrawText("CURRENT PALETTE:", 60, 15, 20, RAYWHITE)
  95. DrawText(paletteText[currentPalette], 300, 15, 20, RED)
  96. DrawFPS(700, 15)
  97. EndDrawing()
  98. '----------------------------------------------------------------------------------
  99. Wend
  100. ' De-Initialization
  101. '--------------------------------------------------------------------------------------
  102. UnloadShader(shader) ' Unload shader
  103. CloseWindow() ' Close window and OpenGL context
  104. '--------------------------------------------------------------------------------------