test/testdrawchessboard.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 07 Dec 2017 16:08:09 -0800
changeset 11730 ac6c607e065c
parent 11274 daea00fb0938
child 11811 5d94cb6b24d3
permissions -rw-r--r--
Enable building the Metal renderer by default, and weak link the Metal framework so the SDL library is safe to use on older Macs
Also generate iOS versions of the Metal shaders
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;
slouken@11274
    28
SDL_Surface *surface;
icculus@9278
    29
int done;
icculus@9278
    30
slouken@7905
    31
void
slouken@7905
    32
DrawChessBoard(SDL_Renderer * renderer)
slouken@7905
    33
{
philipp@9922
    34
    int row = 0,column = 0,x = 0;
philipp@9922
    35
    SDL_Rect rect, darea;
slouken@7905
    36
philipp@9922
    37
    /* Get the Size of drawing surface */
philipp@9922
    38
    SDL_RenderGetViewport(renderer, &darea);
slouken@7905
    39
philipp@9922
    40
    for( ; row < 8; row++)
philipp@9922
    41
    {
philipp@9922
    42
        column = row%2;
philipp@9922
    43
        x = column;
philipp@9922
    44
        for( ; column < 4+(row%2); column++)
philipp@9922
    45
        {
philipp@9922
    46
            SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0xFF);
slouken@7905
    47
philipp@9922
    48
            rect.w = darea.w/8;
philipp@9922
    49
            rect.h = darea.h/8;
philipp@9922
    50
            rect.x = x * rect.w;
philipp@9922
    51
            rect.y = row * rect.h;
philipp@9922
    52
            x = x + 2;
philipp@9922
    53
            SDL_RenderFillRect(renderer, &rect);
philipp@9922
    54
        }
philipp@9922
    55
    }
slouken@7905
    56
}
slouken@7905
    57
icculus@9278
    58
void
icculus@9278
    59
loop()
icculus@9278
    60
{
icculus@9278
    61
    SDL_Event e;
philipp@9605
    62
    while (SDL_PollEvent(&e)) {
slouken@11274
    63
       
slouken@11274
    64
       /* Re-create when window has been resized */
slouken@11274
    65
       if ((e.type == SDL_WINDOWEVENT) && (e.window.event == SDL_WINDOWEVENT_SIZE_CHANGED)) {
slouken@11274
    66
slouken@11274
    67
          SDL_DestroyRenderer(renderer);
slouken@11274
    68
slouken@11274
    69
          surface = SDL_GetWindowSurface(window);
slouken@11274
    70
          renderer = SDL_CreateSoftwareRenderer(surface);
slouken@11274
    71
          /* Clear the rendering surface with the specified color */
slouken@11274
    72
          SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
slouken@11274
    73
          SDL_RenderClear(renderer);
slouken@11274
    74
       }
slouken@11274
    75
slouken@11274
    76
       if (e.type == SDL_QUIT) {
philipp@9922
    77
            done = 1;
philipp@9607
    78
#ifdef __EMSCRIPTEN__
philipp@9922
    79
            emscripten_cancel_main_loop();
philipp@9607
    80
#endif
philipp@9922
    81
            return;
philipp@9922
    82
        }
icculus@9278
    83
philipp@9922
    84
        if ((e.type == SDL_KEYDOWN) && (e.key.keysym.sym == SDLK_ESCAPE)) {
philipp@9922
    85
            done = 1;
philipp@9607
    86
#ifdef __EMSCRIPTEN__
philipp@9922
    87
            emscripten_cancel_main_loop();
philipp@9607
    88
#endif
philipp@9922
    89
            return;
philipp@9922
    90
        }
philipp@9922
    91
    }
philipp@9922
    92
philipp@9922
    93
    DrawChessBoard(renderer);
philipp@9922
    94
philipp@9922
    95
    /* Got everything on rendering surface,
philipp@9922
    96
       now Update the drawing image on window screen */
philipp@9922
    97
    SDL_UpdateWindowSurface(window);
icculus@9278
    98
}
icculus@9278
    99
slouken@7905
   100
int
slouken@7905
   101
main(int argc, char *argv[])
slouken@7905
   102
{
aschiffler@7639
   103
    /* Enable standard application logging */
slouken@7905
   104
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
slouken@7905
   105
philipp@9922
   106
    /* Initialize SDL */
philipp@9922
   107
    if(SDL_Init(SDL_INIT_VIDEO) != 0)
philipp@9922
   108
    {
philipp@9922
   109
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_Init fail : %s\n", SDL_GetError());
philipp@9922
   110
        return 1;
philipp@9922
   111
    }
slouken@7905
   112
slouken@7905
   113
philipp@9922
   114
    /* Create window and renderer for given surface */
slouken@11274
   115
    window = SDL_CreateWindow("Chess Board", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_RESIZABLE);
philipp@9922
   116
    if(!window)
philipp@9922
   117
    {
philipp@9922
   118
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Window creation fail : %s\n",SDL_GetError());
philipp@9922
   119
        return 1;
philipp@9922
   120
    }
philipp@9922
   121
    surface = SDL_GetWindowSurface(window);
philipp@9922
   122
    renderer = SDL_CreateSoftwareRenderer(surface);
philipp@9922
   123
    if(!renderer)
philipp@9922
   124
    {
philipp@9922
   125
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Render creation for surface fail : %s\n",SDL_GetError());
philipp@9922
   126
        return 1;
philipp@9922
   127
    }
slouken@7905
   128
philipp@9922
   129
    /* Clear the rendering surface with the specified color */
philipp@9922
   130
    SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
philipp@9922
   131
    SDL_RenderClear(renderer);
slouken@7905
   132
slouken@7905
   133
philipp@9922
   134
    /* Draw the Image on rendering surface */
philipp@9922
   135
    done = 0;
icculus@9278
   136
#ifdef __EMSCRIPTEN__
icculus@9278
   137
    emscripten_set_main_loop(loop, 0, 1);
icculus@9278
   138
#else
icculus@9278
   139
    while (!done) {
icculus@9278
   140
        loop();
philipp@9922
   141
    }
icculus@9278
   142
#endif
slouken@7905
   143
philipp@9836
   144
    SDL_Quit();
philipp@9922
   145
    return 0;
slouken@7905
   146
}
slouken@7905
   147