'' SDL2-Example program:
'' Draw a Chess Board by using SDL_CreateSoftwareRenderer API
'' translated by TJF 2013
'/*
   '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.

   'This file is created by : Nitin Jain (nitin.j4@samsung.com)
'*/

#INCLUDE ONCE "SDL2/SDL2.bi"

SUB DrawChessBoard(BYVAL Renderer AS SDL_Renderer PTR)
  DIM AS SDL_Rect rect, darea

  '/* Get the Size of drawing surface */
  SDL_RenderGetViewport(Renderer, @darea)

  VAR x = 0, row = 0
  FOR i AS INTEGER = row TO 7
    VAR coloum = row MOD 2
    x += coloum
    FOR j AS INTEGER = 1 TO 4 + (row MOD 2)
      SDL_SetRenderDrawColor(Renderer, 0, 0, 0, 0)

      rect.w = darea.w / 8
      rect.h = darea.h / 8
      rect.x = x * rect.w
      rect.y = row * rect.h
      x += 2
      SDL_RenderFillRect(Renderer, @rect)
    NEXT
    x = 0
  NEXT
END SUB


'/* Enable standard application logging */
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO)

'/* Initialize SDL */
IF SDL_Init(SDL_INIT_VIDEO) < 0 THEN
  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, _
    !"Couldn't initialize SDL: %s\n", SDL_GetError())
  END 2
END IF

'/* Create window and renderer for given surface */
VAR windw = SDL_CreateWindow("Chess Board", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN)
IF 0 = windw THEN
  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, !"Window creation fail: %s\n", SDL_GetError())
  END 1
END IF

VAR surface = SDL_GetWindowSurface(windw)
VAR renderer = SDL_CreateSoftwareRenderer(surface)
IF 0 = renderer THEN
  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, !"Render creation for surface fail: %s\n", SDL_GetError())
  END 1
END IF

'/* Clear the rendering surface with the specified color */
SDL_SetRenderDrawColor(renderer, &hFF, &hFF, &hFF, 0)
SDL_RenderClear(renderer)

DIM AS SDL_Event event
'/* Draw the Image on rendering surface */
WHILE 1
  IF SDL_PollEvent(@event) THEN
    IF event.type = SDL_QUIT_ THEN EXIT WHILE
    IF event.key.keysym.sym = SDLK_ESCAPE THEN EXIT WHILE
  END IF

  DrawChessBoard(renderer)

  '/* Got everything on rendering surface,
    'now Update the drawing image on window screen */
  SDL_UpdateWindowSurface(windw)
WEND

