test/testime.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 31 Dec 2012 10:26:38 -0800
changeset 6785 c094c438c0a6
parent 6318 49b2cb56db6e
child 7191 75360622e65f
permissions -rw-r--r--
Switched the test code over to use the common functions in the test library.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5244
diff changeset
     1
/*
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5244
diff changeset
     2
  Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5244
diff changeset
     3
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5244
diff changeset
     4
  This software is provided 'as-is', without any express or implied
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5244
diff changeset
     5
  warranty.  In no event will the authors be held liable for any damages
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5244
diff changeset
     6
  arising from the use of this software.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5244
diff changeset
     7
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5244
diff changeset
     8
  Permission is granted to anyone to use this software for any purpose,
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5244
diff changeset
     9
  including commercial applications, and to alter it and redistribute it
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5244
diff changeset
    10
  freely.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5244
diff changeset
    11
*/
6251
3e8c673cad58 Removing some more references to SDL 1.3
Sam Lantinga <slouken@libsdl.org>
parents: 5535
diff changeset
    12
/* A simple program to test the Input Method support in the SDL library (2.0+) */
3338
9de326b3099c Fixed bug #817
Sam Lantinga <slouken@libsdl.org>
parents: 3281
diff changeset
    13
3280
00cace2d9080 Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    14
#include <stdlib.h>
00cace2d9080 Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    15
#include <stdio.h>
00cace2d9080 Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    16
#include <string.h>
00cace2d9080 Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    17
00cace2d9080 Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    18
#include "SDL.h"
00cace2d9080 Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    19
#ifdef HAVE_SDL_TTF
00cace2d9080 Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    20
#include "SDL_ttf.h"
00cace2d9080 Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    21
#endif
3338
9de326b3099c Fixed bug #817
Sam Lantinga <slouken@libsdl.org>
parents: 3281
diff changeset
    22
6785
c094c438c0a6 Switched the test code over to use the common functions in the test library.
Sam Lantinga <slouken@libsdl.org>
parents: 6318
diff changeset
    23
#include "SDL_test_common.h"
6318
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
    24
3280
00cace2d9080 Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    25
#define DEFAULT_PTSIZE  30
5150
1435f8a6425c Nobody is currently maintaining the QNX code, so removing it for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4763
diff changeset
    26
#define DEFAULT_FONT    "/System/Library/Fonts/华文细黑.ttf"
3280
00cace2d9080 Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    27
#define MAX_TEXT_LENGTH 256
00cace2d9080 Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    28
6785
c094c438c0a6 Switched the test code over to use the common functions in the test library.
Sam Lantinga <slouken@libsdl.org>
parents: 6318
diff changeset
    29
static SDLTest_CommonState *state;
6318
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
    30
static SDL_Rect textRect, markedRect;
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
    31
static SDL_Color lineColor = {0,0,0,0};
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
    32
static SDL_Color backColor = {255,255,255,0};
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
    33
static SDL_Color textColor = {0,0,0,0};
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
    34
static char text[MAX_TEXT_LENGTH], markedText[SDL_TEXTEDITINGEVENT_TEXT_SIZE];
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
    35
static int cursor = 0;
3280
00cace2d9080 Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    36
#ifdef HAVE_SDL_TTF
6318
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
    37
static TTF_Font *font;
3280
00cace2d9080 Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    38
#endif
4757
140be6839185 Correctly draw cursor based on position.
dewyatt
parents: 4756
diff changeset
    39
5150
1435f8a6425c Nobody is currently maintaining the QNX code, so removing it for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4763
diff changeset
    40
size_t utf8_length(unsigned char c)
1435f8a6425c Nobody is currently maintaining the QNX code, so removing it for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4763
diff changeset
    41
{
1435f8a6425c Nobody is currently maintaining the QNX code, so removing it for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4763
diff changeset
    42
    c = (unsigned char)(0xff & c);
1435f8a6425c Nobody is currently maintaining the QNX code, so removing it for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4763
diff changeset
    43
    if (c < 0x80)
1435f8a6425c Nobody is currently maintaining the QNX code, so removing it for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4763
diff changeset
    44
        return 1;
1435f8a6425c Nobody is currently maintaining the QNX code, so removing it for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4763
diff changeset
    45
    else if ((c >> 5) ==0x6)
1435f8a6425c Nobody is currently maintaining the QNX code, so removing it for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4763
diff changeset
    46
        return 2;
1435f8a6425c Nobody is currently maintaining the QNX code, so removing it for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4763
diff changeset
    47
    else if ((c >> 4) == 0xe)
1435f8a6425c Nobody is currently maintaining the QNX code, so removing it for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4763
diff changeset
    48
        return 3;
1435f8a6425c Nobody is currently maintaining the QNX code, so removing it for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4763
diff changeset
    49
    else if ((c >> 3) == 0x1e)
1435f8a6425c Nobody is currently maintaining the QNX code, so removing it for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4763
diff changeset
    50
        return 4;
1435f8a6425c Nobody is currently maintaining the QNX code, so removing it for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4763
diff changeset
    51
    else
1435f8a6425c Nobody is currently maintaining the QNX code, so removing it for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4763
diff changeset
    52
        return 0;
1435f8a6425c Nobody is currently maintaining the QNX code, so removing it for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4763
diff changeset
    53
}
1435f8a6425c Nobody is currently maintaining the QNX code, so removing it for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4763
diff changeset
    54
1435f8a6425c Nobody is currently maintaining the QNX code, so removing it for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4763
diff changeset
    55
char *utf8_next(char *p)
1435f8a6425c Nobody is currently maintaining the QNX code, so removing it for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4763
diff changeset
    56
{
1435f8a6425c Nobody is currently maintaining the QNX code, so removing it for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4763
diff changeset
    57
    size_t len = utf8_length(*p);
1435f8a6425c Nobody is currently maintaining the QNX code, so removing it for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4763
diff changeset
    58
    size_t i = 0;
1435f8a6425c Nobody is currently maintaining the QNX code, so removing it for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4763
diff changeset
    59
    if (!len)
1435f8a6425c Nobody is currently maintaining the QNX code, so removing it for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4763
diff changeset
    60
        return 0;
1435f8a6425c Nobody is currently maintaining the QNX code, so removing it for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4763
diff changeset
    61
1435f8a6425c Nobody is currently maintaining the QNX code, so removing it for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4763
diff changeset
    62
    for (; i < len; ++i)
1435f8a6425c Nobody is currently maintaining the QNX code, so removing it for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4763
diff changeset
    63
    {
1435f8a6425c Nobody is currently maintaining the QNX code, so removing it for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4763
diff changeset
    64
        ++p;
1435f8a6425c Nobody is currently maintaining the QNX code, so removing it for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4763
diff changeset
    65
        if (!*p)
1435f8a6425c Nobody is currently maintaining the QNX code, so removing it for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4763
diff changeset
    66
            return 0;
1435f8a6425c Nobody is currently maintaining the QNX code, so removing it for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4763
diff changeset
    67
    }
1435f8a6425c Nobody is currently maintaining the QNX code, so removing it for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4763
diff changeset
    68
    return p;
1435f8a6425c Nobody is currently maintaining the QNX code, so removing it for now.
Sam Lantinga <slouken@libsdl.org>
parents: 4763
diff changeset
    69
}
4757
140be6839185 Correctly draw cursor based on position.
dewyatt
parents: 4756
diff changeset
    70
140be6839185 Correctly draw cursor based on position.
dewyatt
parents: 4756
diff changeset
    71
char *utf8_advance(char *p, size_t distance)
140be6839185 Correctly draw cursor based on position.
dewyatt
parents: 4756
diff changeset
    72
{
140be6839185 Correctly draw cursor based on position.
dewyatt
parents: 4756
diff changeset
    73
    size_t i = 0;
140be6839185 Correctly draw cursor based on position.
dewyatt
parents: 4756
diff changeset
    74
    for (; i < distance && p; ++i)
140be6839185 Correctly draw cursor based on position.
dewyatt
parents: 4756
diff changeset
    75
    {
140be6839185 Correctly draw cursor based on position.
dewyatt
parents: 4756
diff changeset
    76
        p = utf8_next(p);
140be6839185 Correctly draw cursor based on position.
dewyatt
parents: 4756
diff changeset
    77
    }
140be6839185 Correctly draw cursor based on position.
dewyatt
parents: 4756
diff changeset
    78
    return p;
140be6839185 Correctly draw cursor based on position.
dewyatt
parents: 4756
diff changeset
    79
}
3280
00cace2d9080 Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    80
00cace2d9080 Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    81
void usage()
00cace2d9080 Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    82
{
6318
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
    83
    printf("usage: testime [--font fontfile]\n");
3280
00cace2d9080 Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    84
    exit(0);
00cace2d9080 Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    85
}
00cace2d9080 Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    86
6318
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
    87
void InitInput()
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
    88
{
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
    89
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
    90
    /* Prepare a rect for text input */
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
    91
    textRect.x = textRect.y = 100;
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
    92
    textRect.w = DEFAULT_WINDOW_WIDTH - 2 * textRect.x;
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
    93
    textRect.h = 50;
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
    94
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
    95
    text[0] = 0;
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
    96
    markedRect = textRect;
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
    97
    markedText[0] = 0;
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
    98
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
    99
    SDL_StartTextInput();
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   100
}
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   101
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   102
void CleanupVideo()
3280
00cace2d9080 Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   103
{
6318
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   104
    SDL_StopTextInput();
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   105
#ifdef HAVE_SDL_TTF
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   106
    TTF_CloseFont(font);
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   107
    TTF_Quit();
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   108
#endif
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   109
}
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   110
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   111
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   112
void _Redraw(SDL_Renderer * renderer) {
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   113
    int w = 0, h = textRect.h;
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   114
    SDL_Rect cursorRect, underlineRect;
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   115
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   116
    SDL_SetRenderDrawColor(renderer, 255,255,255,255);
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   117
    SDL_RenderFillRect(renderer,&textRect);
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   118
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   119
#ifdef HAVE_SDL_TTF
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   120
    if (*text)
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   121
    {
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   122
        SDL_Surface *textSur = TTF_RenderUTF8_Blended(font, text, textColor);
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   123
        SDL_Rect dest = {textRect.x, textRect.y, textSur->w, textSur->h };
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   124
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   125
        SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer,textSur);
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   126
        SDL_FreeSurface(textSur);
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   127
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   128
        SDL_RenderCopy(renderer,texture,NULL,&dest);
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   129
        SDL_DestroyTexture(texture);
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   130
        TTF_SizeUTF8(font, text, &w, &h);
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   131
    }
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   132
#endif
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   133
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   134
    markedRect.x = textRect.x + w;
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   135
    markedRect.w = textRect.w - w;
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   136
    if (markedRect.w < 0)
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   137
    {
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   138
        // Stop text input because we cannot hold any more characters
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   139
        SDL_StopTextInput();
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   140
        return;
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   141
    }
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   142
    else
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   143
    {
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   144
        SDL_StartTextInput();
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   145
    }
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   146
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   147
    cursorRect = markedRect;
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   148
    cursorRect.w = 2;
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   149
    cursorRect.h = h;
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   150
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   151
    SDL_SetRenderDrawColor(renderer, 255,255,255,255);
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   152
    SDL_RenderFillRect(renderer,&markedRect);
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   153
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   154
    if (markedText[0])
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   155
    {
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   156
#ifdef HAVE_SDL_TTF
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   157
        if (cursor)
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   158
        {
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   159
            char *p = utf8_advance(markedText, cursor);
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   160
            char c = 0;
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   161
            if (!p)
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   162
                p = &markedText[strlen(markedText)];
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   163
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   164
            c = *p;
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   165
            *p = 0;
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   166
            TTF_SizeUTF8(font, markedText, &w, 0);
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   167
            cursorRect.x += w;
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   168
            *p = c;
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   169
        }
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   170
        SDL_Surface *textSur = TTF_RenderUTF8_Blended(font, markedText, textColor);
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   171
        SDL_Rect dest = {markedRect.x, markedRect.y, textSur->w, textSur->h };
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   172
        TTF_SizeUTF8(font, markedText, &w, &h);
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   173
        SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer,textSur);
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   174
        SDL_FreeSurface(textSur);
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   175
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   176
        SDL_RenderCopy(renderer,texture,NULL,&dest);
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   177
        SDL_DestroyTexture(texture);
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   178
#endif
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   179
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   180
        underlineRect = markedRect;
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   181
        underlineRect.y += (h - 2);
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   182
        underlineRect.h = 2;
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   183
        underlineRect.w = w;
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   184
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   185
        SDL_SetRenderDrawColor(renderer, 0,0,0,0);
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   186
        SDL_RenderFillRect(renderer,&markedRect);
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   187
    }
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   188
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   189
    SDL_SetRenderDrawColor(renderer, 0,0,0,0);
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   190
    SDL_RenderFillRect(renderer,&cursorRect);
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   191
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   192
    SDL_SetTextInputRect(&markedRect);
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   193
}
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   194
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   195
void Redraw() {
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   196
    int i;
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   197
    for (i = 0; i < state->num_windows; ++i) {
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   198
        SDL_Renderer *renderer = state->renderers[i];
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   199
        SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   200
        SDL_RenderClear(renderer);
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   201
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   202
        _Redraw(renderer);
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   203
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   204
        SDL_RenderPresent(renderer);
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   205
    }
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   206
}
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   207
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   208
int main(int argc, char *argv[]) {
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   209
    int i, done;
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   210
    SDL_Event event;
3280
00cace2d9080 Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   211
    const char *fontname = DEFAULT_FONT;
00cace2d9080 Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   212
6318
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   213
    /* Initialize test framework */
6785
c094c438c0a6 Switched the test code over to use the common functions in the test library.
Sam Lantinga <slouken@libsdl.org>
parents: 6318
diff changeset
   214
    state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
6318
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   215
    if (!state) {
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   216
        return 1;
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   217
    }
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   218
    for (i = 1; i < argc;i++) {
6785
c094c438c0a6 Switched the test code over to use the common functions in the test library.
Sam Lantinga <slouken@libsdl.org>
parents: 6318
diff changeset
   219
        SDLTest_CommonArg(state, i);
6318
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   220
    }
3280
00cace2d9080 Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   221
    for (argc--, argv++; argc > 0; argc--, argv++)
00cace2d9080 Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   222
    {
6318
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   223
        if (strcmp(argv[0], "--help") == 0) {
3280
00cace2d9080 Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   224
            usage();
6318
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   225
            return 0;
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   226
        }
3280
00cace2d9080 Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   227
00cace2d9080 Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   228
        else if (strcmp(argv[0], "--font") == 0)
00cace2d9080 Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   229
        {
00cace2d9080 Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   230
            argc--;
00cace2d9080 Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   231
            argv++;
00cace2d9080 Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   232
00cace2d9080 Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   233
            if (argc > 0)
00cace2d9080 Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   234
                fontname = argv[0];
6318
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   235
            else {
3280
00cace2d9080 Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   236
                usage();
6318
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   237
                return 0;
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   238
            }
3280
00cace2d9080 Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   239
        }
00cace2d9080 Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   240
    }
6318
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   241
    
6785
c094c438c0a6 Switched the test code over to use the common functions in the test library.
Sam Lantinga <slouken@libsdl.org>
parents: 6318
diff changeset
   242
    if (!SDLTest_CommonInit(state)) {
6318
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   243
        return 2;
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   244
    }
3280
00cace2d9080 Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   245
00cace2d9080 Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   246
00cace2d9080 Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   247
#ifdef HAVE_SDL_TTF
00cace2d9080 Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   248
    /* Initialize fonts */
00cace2d9080 Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   249
    TTF_Init();
00cace2d9080 Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   250
00cace2d9080 Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   251
    font = TTF_OpenFont(fontname, DEFAULT_PTSIZE);
00cace2d9080 Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   252
    if (! font)
00cace2d9080 Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   253
    {
3281
2d3a9f229ba1 I'm gussing that this was never compiled where TTF support was missing. As a result a call to SDL_GetError and a call to TTF_GetError were swapped.
Bob Pendleton <bob@pendleton.com>
parents: 3280
diff changeset
   254
        fprintf(stderr, "Failed to find font: %s\n", TTF_GetError());
3280
00cace2d9080 Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   255
        exit(-1);
00cace2d9080 Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   256
    }
00cace2d9080 Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   257
#endif
00cace2d9080 Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   258
00cace2d9080 Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   259
    printf("Using font: %s\n", fontname);
00cace2d9080 Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   260
    atexit(SDL_Quit);
00cace2d9080 Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   261
6318
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   262
    InitInput();
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   263
    /* Create the windows and initialize the renderers */
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   264
    for (i = 0; i < state->num_windows; ++i) {
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   265
        SDL_Renderer *renderer = state->renderers[i];
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   266
        SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE);
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   267
        SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   268
        SDL_RenderClear(renderer);
3280
00cace2d9080 Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   269
    }
6318
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   270
    Redraw();    
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   271
    /* Main render loop */
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   272
    done = 0;
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   273
    while (!done) {
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   274
        /* Check for events */
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   275
        while (SDL_PollEvent(&event)) {
6785
c094c438c0a6 Switched the test code over to use the common functions in the test library.
Sam Lantinga <slouken@libsdl.org>
parents: 6318
diff changeset
   276
            SDLTest_CommonEvent(state, &event, &done);
6318
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   277
            switch(event.type) {
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   278
                case SDL_KEYDOWN: {
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   279
                    switch (event.key.keysym.sym)
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   280
                    {
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   281
                        case SDLK_RETURN:
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   282
                             text[0]=0x00;
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   283
                             Redraw();
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   284
                             break;
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   285
                        case SDLK_BACKSPACE:
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   286
                             {
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   287
                                 int textlen=SDL_strlen(text);
3280
00cace2d9080 Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   288
6318
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   289
                                 do {
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   290
                                     if (textlen==0)
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   291
                                     {
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   292
                                         break;
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   293
                                     }
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   294
                                     if ((text[textlen-1] & 0x80) == 0x00)
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   295
                                     {
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   296
                                         /* One byte */
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   297
                                         text[textlen-1]=0x00;
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   298
                                         break;
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   299
                                     }
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   300
                                     if ((text[textlen-1] & 0xC0) == 0x80)
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   301
                                     {
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   302
                                         /* Byte from the multibyte sequence */
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   303
                                         text[textlen-1]=0x00;
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   304
                                         textlen--;
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   305
                                     }
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   306
                                     if ((text[textlen-1] & 0xC0) == 0xC0)
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   307
                                     {
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   308
                                         /* First byte of multibyte sequence */
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   309
                                         text[textlen-1]=0x00;
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   310
                                         break;
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   311
                                     }
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   312
                                 } while(1);
3280
00cace2d9080 Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   313
6318
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   314
                                 Redraw();
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   315
                             }
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   316
                             break;
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   317
                    }
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   318
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   319
                    if (done)
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   320
                    {
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   321
                        break;
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   322
                    }
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   323
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   324
                    fprintf(stderr,
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   325
                            "Keyboard: scancode 0x%08X = %s, keycode 0x%08X = %s\n",
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   326
                            event.key.keysym.scancode,
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   327
                            SDL_GetScancodeName(event.key.keysym.scancode),
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   328
                            event.key.keysym.sym, SDL_GetKeyName(event.key.keysym.sym));
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   329
                    break;
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   330
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   331
                case SDL_TEXTINPUT:
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   332
                    if (SDL_strlen(event.text.text) == 0 || event.text.text[0] == '\n' ||
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   333
                        markedRect.w < 0)
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   334
                        break;
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   335
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   336
                    fprintf(stderr, "Keyboard: text input \"%s\"\n", event.text.text);
3280
00cace2d9080 Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   337
6318
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   338
                    if (SDL_strlen(text) + SDL_strlen(event.text.text) < sizeof(text))
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   339
                        SDL_strlcat(text, event.text.text, sizeof(text));
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   340
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   341
                    fprintf(stderr, "text inputed: %s\n", text);
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   342
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   343
                    // After text inputed, we can clear up markedText because it
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   344
                    // is committed
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   345
                    markedText[0] = 0;
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   346
                    Redraw();
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   347
                    break;
3280
00cace2d9080 Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   348
6318
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   349
                case SDL_TEXTEDITING:
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   350
                    fprintf(stderr, "text editing \"%s\", selected range (%d, %d)\n",
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   351
                            event.edit.text, event.edit.start, event.edit.length);
3280
00cace2d9080 Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   352
6318
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   353
                    strcpy(markedText, event.edit.text);
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   354
                    cursor = event.edit.start;
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   355
                    Redraw();
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   356
                    break;
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   357
                }
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   358
                break;
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   359
                    
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   360
            }
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   361
        }
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   362
    }
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   363
    CleanupVideo();
6785
c094c438c0a6 Switched the test code over to use the common functions in the test library.
Sam Lantinga <slouken@libsdl.org>
parents: 6318
diff changeset
   364
    SDLTest_CommonQuit(state);
6318
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6256
diff changeset
   365
    return 0;
3280
00cace2d9080 Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   366
}
00cace2d9080 Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   367
5244
762e40fb8e28 Be explicit about what display you're querying. The default display is 0.
Sam Lantinga <slouken@libsdl.org>
parents: 5150
diff changeset
   368
762e40fb8e28 Be explicit about what display you're querying. The default display is 0.
Sam Lantinga <slouken@libsdl.org>
parents: 5150
diff changeset
   369
/* vi: set ts=4 sw=4 expandtab: */