'' SDL2_mixer Example program:
'' A test application for the SDL image loading library.
'' translated/modified by TJF 2013
'/*
  'showimage:  A test application for the SDL image loading library.
  'Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>

  'This software is provided 'as-is', without any express or implied
  'warranty.  In no event will the authors be held liable for any damages
  'arising from the use of this software.

  'Permission is granted to anyone to use this software for any purpose,
  'including commercial applications, and to alter it and redistribute it
  'freely, subject to the following restrictions:

  '1. The origin of this software must not be misrepresented; you must not
     'claim that you wrote the original software. If you use this software
     'in a product, an acknowledgment in the product documentation would be
     'appreciated but is not required.
  '2. Altered source versions must be plainly marked as such, and must not be
     'misrepresented as being the original software.
  '3. This notice may not be removed or altered from any source distribution.
'*/

#INCLUDE ONCE "SDL2/SDL2.bi"
#INCLUDE ONCE "SDL2/SDL2_image.bi"

'/* Draw a Gimpish background pattern to show transparency in the image */
SUB draw_background(BYVAL Renderer AS SDL_Renderer PTR, BYVAL W AS INTEGER, BYVAL H AS INTEGER)
  STATIC AS SDL_Color col(1) = { _
    TYPE( &h66, &h66, &h66, &hff ), _
    TYPE( &h99, &h99, &h99, &hff ) _
  }
  DIM AS SDL_Rect rect

  rect.w = 8
  rect.h = 8
  FOR y AS INTEGER = 0 TO H - 1
    FOR x AS INTEGER = 0 TO W - 1
      '/* use an 8x8 checkerboard pattern */
      VAR i = (((x ^ y) SHR 3) AND 1)
      SDL_SetRenderDrawColor(Renderer, col(i).r, col(i).g, col(i).b, col(i).a)

      rect.x = x
      rect.y = y
      SDL_RenderFillRect(Renderer, @rect)
    NEXT
  NEXT
END SUB

'/* Check command line usage */
IF 0 = __FB_ARGV__[1] THEN
  fprintf(stderr, !"Usage: %s [-fullscreen] [-save file.png] <image_file> ...\n", __FB_ARGV__[0])
  END 1
END IF

DIM AS Uint32 flags = SDL_WINDOW_HIDDEN
VAR i = 1
WHILE __FB_ARGV__[i]
  IF *__FB_ARGV__[i] = "-fullscreen" THEN
    SDL_ShowCursor(0)
    flags OR= SDL_WINDOW_FULLSCREEN
  END IF
  i += 1
WEND

DIM AS SDL_Window PTR windw
DIM AS SDL_Renderer PTR renderer
IF SDL_CreateWindowAndRenderer(0, 0, flags, @windw, @renderer) < 0 THEN
  fprintf(stderr, !"SDL_CreateWindowAndRenderer() failed: %s\n", SDL_GetError())
  END 2
END IF

i = 1
DIM AS INTEGER w, h
DIM AS CONST ZSTRING PTR saveFile = NULL
WHILE __FB_ARGV__[i]
  IF *__FB_ARGV__[i] = "-fullscreen" THEN i += 1 : CONTINUE WHILE

  IF *__FB_ARGV__[i] = "-save" ANDALSO __FB_ARGV__[i + 1] <> 0 THEN
    i += 1
    saveFile = __FB_ARGV__[i]
    i += 1 : CONTINUE WHILE
  END IF

  '/* Open the image file */
  VAR texture = IMG_LoadTexture(renderer, __FB_ARGV__[i])
  IF 0 = texture THEN
    fprintf(stderr, !"Couldn't load %s: %s\n", __FB_ARGV__[i], SDL_GetError())
    i += 1 : CONTINUE WHILE
  END IF
  SDL_QueryTexture(texture, NULL, NULL, @w, @h)

  '/* Save the image file, if desired */
  IF saveFile THEN
    VAR surface = IMG_Load(__FB_ARGV__[i])
    IF surface THEN
      IF IMG_SavePNG(surface, saveFile) < 0 THEN
        fprintf(stderr, !"Couldn't save %s: %s\n", saveFile, SDL_GetError())
      END IF
    ELSE
      fprintf(stderr, !"Couldn't load %s: %s\n", __FB_ARGV__[i], SDL_GetError())
    END IF
  END IF

  '/* Show the window */
  SDL_SetWindowTitle(windw, __FB_ARGV__[i])
  SDL_SetWindowSize(windw, w, h)
  SDL_ShowWindow(windw)

  VAR done = 0
  DIM AS SDL_Event event
  WHILE 0 = done
    WHILE SDL_PollEvent(@event)
      SELECT CASE event.type
      CASE SDL_KEYUP
        SELECT CASE event.key.keysym.sym
          CASE SDLK_LEFT   : IF i > 1 THEN i -= 2 : done = 1
          CASE SDLK_RIGHT  : IF __FB_ARGV__[i + 1] THEN done = 1
          CASE SDLK_ESCAPE
          CASE SDLK_q      : __FB_ARGV__[i + 1] = NULL

          CASE SDLK_SPACE  '/* Drop through to done */
          CASE SDLK_TAB    : done = 1
        END SELECT
      CASE SDL_MOUSEBUTTONDOWN : done = 1
      CASE SDL_QUIT_           : __FB_ARGV__[i + 1] = NULL : done = 1
      END SELECT
    WEND
    '/* Draw a background pattern in case the image has transparency */
    draw_background(renderer, w, h)

    '/* Display the image */
    SDL_RenderCopy(renderer, texture, NULL, NULL)
    SDL_RenderPresent(renderer)

    SDL_Delay(100)
  WEND
  SDL_DestroyTexture(texture)
  i += 1
WEND

'/* We're done! */
SDL_Quit()
