test/testdrawchessboard.c
author Ryan C. Gordon <icculus@icculus.org>
Sat, 07 Jan 2017 22:24:45 -0500
changeset 10782 1de6d4633ee6
parent 10737 3406a0f8b041
child 11274 daea00fb0938
permissions -rw-r--r--
Fixed a bunch of compiler warnings in the test code.
slouken@7905
     1
/*
slouken@10737
     2
   Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
slouken@7905
     3
slouken@7905
     4
   This software is provided 'as-is', without any express or implied
slouken@7905
     5
   warranty.  In no event will the authors be held liable for any damages
slouken@7905
     6
   arising from the use of this software.
slouken@7905
     7
slouken@7905
     8
   Permission is granted to anyone to use this software for any purpose,
slouken@7905
     9
   including commercial applications, and to alter it and redistribute it
slouken@7905
    10
   freely.
slouken@7905
    11
slouken@7905
    12
   This file is created by : Nitin Jain (nitin.j4@samsung.com)
slouken@7905
    13
*/
slouken@7905
    14
slouken@7905
    15
/* Sample program:  Draw a Chess Board  by using SDL_CreateSoftwareRenderer API */
slouken@7905
    16
icculus@9278
    17
#include <stdlib.h>
icculus@9278
    18
#include <stdio.h>
icculus@9278
    19
icculus@9278
    20
#ifdef __EMSCRIPTEN__
icculus@9278
    21
#include <emscripten/emscripten.h>
icculus@9278
    22
#endif
icculus@9278
    23
slouken@7905
    24
#include "SDL.h"
slouken@7905
    25
icculus@9278
    26
SDL_Window *window;
icculus@9278
    27
SDL_Renderer *renderer;
icculus@9278
    28
int done;
icculus@9278
    29
slouken@7905
    30
void
slouken@7905
    31
DrawChessBoard(SDL_Renderer * renderer)
slouken@7905
    32
{
philipp@9922
    33
    int row = 0,column = 0,x = 0;
philipp@9922
    34
    SDL_Rect rect, darea;
slouken@7905
    35
philipp@9922
    36
    /* Get the Size of drawing surface */
philipp@9922
    37
    SDL_RenderGetViewport(renderer, &darea);
slouken@7905
    38
philipp@9922
    39
    for( ; row < 8; row++)
philipp@9922
    40
    {
philipp@9922
    41
        column = row%2;
philipp@9922
    42
        x = column;
philipp@9922
    43
        for( ; column < 4+(row%2); column++)
philipp@9922
    44
        {
philipp@9922
    45
            SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0xFF);
slouken@7905
    46
philipp@9922
    47
            rect.w = darea.w/8;
philipp@9922
    48
            rect.h = darea.h/8;
philipp@9922
    49
            rect.x = x * rect.w;
philipp@9922
    50
            rect.y = row * rect.h;
philipp@9922
    51
            x = x + 2;
philipp@9922
    52
            SDL_RenderFillRect(renderer, &rect);
philipp@9922
    53
        }
philipp@9922
    54
    }
slouken@7905
    55
}
slouken@7905
    56
icculus@9278
    57
void
icculus@9278
    58
loop()
icculus@9278
    59
{
icculus@9278
    60
    SDL_Event e;
philipp@9605
    61
    while (SDL_PollEvent(&e)) {
philipp@9922
    62
        if (e.type == SDL_QUIT) {
philipp@9922
    63
            done = 1;
philipp@9607
    64
#ifdef __EMSCRIPTEN__
philipp@9922
    65
            emscripten_cancel_main_loop();
philipp@9607
    66
#endif
philipp@9922
    67
            return;
philipp@9922
    68
        }
icculus@9278
    69
philipp@9922
    70
        if ((e.type == SDL_KEYDOWN) && (e.key.keysym.sym == SDLK_ESCAPE)) {
philipp@9922
    71
            done = 1;
philipp@9607
    72
#ifdef __EMSCRIPTEN__
philipp@9922
    73
            emscripten_cancel_main_loop();
philipp@9607
    74
#endif
philipp@9922
    75
            return;
philipp@9922
    76
        }
philipp@9922
    77
    }
philipp@9922
    78
philipp@9922
    79
    DrawChessBoard(renderer);
philipp@9922
    80
philipp@9922
    81
    /* Got everything on rendering surface,
philipp@9922
    82
       now Update the drawing image on window screen */
philipp@9922
    83
    SDL_UpdateWindowSurface(window);
icculus@9278
    84
}
icculus@9278
    85
slouken@7905
    86
int
slouken@7905
    87
main(int argc, char *argv[])
slouken@7905
    88
{
philipp@9922
    89
    SDL_Surface *surface;
slouken@7905
    90
aschiffler@7639
    91
    /* Enable standard application logging */
slouken@7905
    92
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
slouken@7905
    93
philipp@9922
    94
    /* Initialize SDL */
philipp@9922
    95
    if(SDL_Init(SDL_INIT_VIDEO) != 0)
philipp@9922
    96
    {
philipp@9922
    97
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_Init fail : %s\n", SDL_GetError());
philipp@9922
    98
        return 1;
philipp@9922
    99
    }
slouken@7905
   100
slouken@7905
   101
philipp@9922
   102
    /* Create window and renderer for given surface */
philipp@10129
   103
    window = SDL_CreateWindow("Chess Board", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0);
philipp@9922
   104
    if(!window)
philipp@9922
   105
    {
philipp@9922
   106
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Window creation fail : %s\n",SDL_GetError());
philipp@9922
   107
        return 1;
philipp@9922
   108
    }
philipp@9922
   109
    surface = SDL_GetWindowSurface(window);
philipp@9922
   110
    renderer = SDL_CreateSoftwareRenderer(surface);
philipp@9922
   111
    if(!renderer)
philipp@9922
   112
    {
philipp@9922
   113
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Render creation for surface fail : %s\n",SDL_GetError());
philipp@9922
   114
        return 1;
philipp@9922
   115
    }
slouken@7905
   116
philipp@9922
   117
    /* Clear the rendering surface with the specified color */
philipp@9922
   118
    SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
philipp@9922
   119
    SDL_RenderClear(renderer);
slouken@7905
   120
slouken@7905
   121
philipp@9922
   122
    /* Draw the Image on rendering surface */
philipp@9922
   123
    done = 0;
icculus@9278
   124
#ifdef __EMSCRIPTEN__
icculus@9278
   125
    emscripten_set_main_loop(loop, 0, 1);
icculus@9278
   126
#else
icculus@9278
   127
    while (!done) {
icculus@9278
   128
        loop();
philipp@9922
   129
    }
icculus@9278
   130
#endif
slouken@7905
   131
philipp@9836
   132
    SDL_Quit();
philipp@9922
   133
    return 0;
slouken@7905
   134
}
slouken@7905
   135