Xcode-iOS/Template/SDL iOS Application/main.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 12 Oct 2012 02:20:10 -0700
changeset 6578 77d70ec131bc
parent 6159 fb6f363032fd
child 7191 75360622e65f
permissions -rw-r--r--
Added SDL_GetRenderTarget() API function Also fixed a bug with setting logical size for a render target.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5277
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
     1
/*
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
     2
 *	rectangles.c
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
     3
 *	written by Holmes Futrell
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
     4
 *	use however you want
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
     5
 */
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
     6
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
     7
#include "SDL.h"
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
     8
#include <time.h>
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
     9
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    10
#define SCREEN_WIDTH 320
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    11
#define SCREEN_HEIGHT 480
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    12
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    13
int
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    14
randomInt(int min, int max)
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    15
{
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    16
    return min + rand() % (max - min + 1);
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    17
}
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    18
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    19
void
5366
d032ec40e7c1 Updated the template code
Sam Lantinga <slouken@libsdl.org>
parents: 5278
diff changeset
    20
render(SDL_Renderer *renderer)
5277
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    21
{
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    22
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    23
    Uint8 r, g, b;
5366
d032ec40e7c1 Updated the template code
Sam Lantinga <slouken@libsdl.org>
parents: 5278
diff changeset
    24
d032ec40e7c1 Updated the template code
Sam Lantinga <slouken@libsdl.org>
parents: 5278
diff changeset
    25
    /* Clear the screen */
5412
279f6853c459 Fixed setting draw color for clear
Sam Lantinga <slouken@libsdl.org>
parents: 5366
diff changeset
    26
    SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
5511
be843abc1f6c Fixed typos in example code
Sam Lantinga <slouken@libsdl.org>
parents: 5412
diff changeset
    27
    SDL_RenderClear(renderer);
5366
d032ec40e7c1 Updated the template code
Sam Lantinga <slouken@libsdl.org>
parents: 5278
diff changeset
    28
5277
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    29
    /*  Come up with a random rectangle */
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    30
    SDL_Rect rect;
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    31
    rect.w = randomInt(64, 128);
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    32
    rect.h = randomInt(64, 128);
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    33
    rect.x = randomInt(0, SCREEN_WIDTH);
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    34
    rect.y = randomInt(0, SCREEN_HEIGHT);
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    35
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    36
    /* Come up with a random color */
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    37
    r = randomInt(50, 255);
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    38
    g = randomInt(50, 255);
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    39
    b = randomInt(50, 255);
5366
d032ec40e7c1 Updated the template code
Sam Lantinga <slouken@libsdl.org>
parents: 5278
diff changeset
    40
    SDL_SetRenderDrawColor(renderer, r, g, b, 255);
5277
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    41
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    42
    /*  Fill the rectangle in the color */
5366
d032ec40e7c1 Updated the template code
Sam Lantinga <slouken@libsdl.org>
parents: 5278
diff changeset
    43
    SDL_RenderFillRect(renderer, &rect);
5277
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    44
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    45
    /* update screen */
5366
d032ec40e7c1 Updated the template code
Sam Lantinga <slouken@libsdl.org>
parents: 5278
diff changeset
    46
    SDL_RenderPresent(renderer);
5277
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    47
}
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    48
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    49
int
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    50
main(int argc, char *argv[])
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    51
{
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    52
5366
d032ec40e7c1 Updated the template code
Sam Lantinga <slouken@libsdl.org>
parents: 5278
diff changeset
    53
    SDL_Window *window;
d032ec40e7c1 Updated the template code
Sam Lantinga <slouken@libsdl.org>
parents: 5278
diff changeset
    54
    SDL_Renderer *renderer;
5277
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    55
    int done;
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    56
    SDL_Event event;
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    57
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    58
    /* initialize SDL */
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    59
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    60
        printf("Could not initialize SDL\n");
5366
d032ec40e7c1 Updated the template code
Sam Lantinga <slouken@libsdl.org>
parents: 5278
diff changeset
    61
        return 1;
5277
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    62
    }
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    63
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    64
    /* seed random number generator */
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    65
    srand(time(NULL));
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    66
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    67
    /* create window and renderer */
5366
d032ec40e7c1 Updated the template code
Sam Lantinga <slouken@libsdl.org>
parents: 5278
diff changeset
    68
    window =
5277
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    69
        SDL_CreateWindow(NULL, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT,
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    70
                         SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
5366
d032ec40e7c1 Updated the template code
Sam Lantinga <slouken@libsdl.org>
parents: 5278
diff changeset
    71
    if (!window) {
5277
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    72
        printf("Could not initialize Window\n");
5366
d032ec40e7c1 Updated the template code
Sam Lantinga <slouken@libsdl.org>
parents: 5278
diff changeset
    73
        return 1;
5277
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    74
    }
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    75
5366
d032ec40e7c1 Updated the template code
Sam Lantinga <slouken@libsdl.org>
parents: 5278
diff changeset
    76
    renderer = SDL_CreateRenderer(window, -1, 0);
5511
be843abc1f6c Fixed typos in example code
Sam Lantinga <slouken@libsdl.org>
parents: 5412
diff changeset
    77
    if (!renderer) {
5366
d032ec40e7c1 Updated the template code
Sam Lantinga <slouken@libsdl.org>
parents: 5278
diff changeset
    78
        printf("Could not create renderer\n");
d032ec40e7c1 Updated the template code
Sam Lantinga <slouken@libsdl.org>
parents: 5278
diff changeset
    79
        return 1;
d032ec40e7c1 Updated the template code
Sam Lantinga <slouken@libsdl.org>
parents: 5278
diff changeset
    80
    }
5277
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    81
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    82
    /* Enter render loop, waiting for user to quit */
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    83
    done = 0;
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    84
    while (!done) {
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    85
        while (SDL_PollEvent(&event)) {
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    86
            if (event.type == SDL_QUIT) {
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    87
                done = 1;
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    88
            }
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    89
        }
5366
d032ec40e7c1 Updated the template code
Sam Lantinga <slouken@libsdl.org>
parents: 5278
diff changeset
    90
        render(renderer);
5277
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    91
        SDL_Delay(1);
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    92
    }
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    93
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    94
    /* shutdown SDL */
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    95
    SDL_Quit();
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    96
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    97
    return 0;
37b582b8bb07 New iOS Xcode target to build libSDL.a and lipo Simulator & Device binaries together.
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff changeset
    98
}