summaryrefslogtreecommitdiffstats
path: root/test/test-client.c
blob: 4cfc842565116eb82aa25098501f7567d1691ac7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "kiro-client.h"
#include "kiro-trb.h"
#include <SDL/SDL.h>
#include <assert.h>


static _Bool 
init_app (const char *name, SDL_Surface *icon, uint32_t flags)
{
    atexit (SDL_Quit);

    if (SDL_Init (flags) < 0)
        return 0;

    SDL_WM_SetCaption (name, name);
    SDL_WM_SetIcon (icon, NULL);
    return 1;
}

static void 
render (SDL_Surface *sf)
{
    SDL_Surface *screen = SDL_GetVideoSurface();

    if (SDL_BlitSurface (sf, NULL, screen, NULL) == 0)
        SDL_UpdateRect (screen, 0, 0, 0, 0);
}

static int 
filter (const SDL_Event *event)
{
    return event->type == SDL_QUIT;
}


int 
main ( int argc, char *argv[] )
{
    if (argc < 3) {
        printf ("Not enough aruments. Usage: 'kiro-client-sdl <address> <port>'\n");
        return -1;
    }

    KiroClient *client = kiro_client_new ();
    KiroTrb *trb = kiro_trb_new ();

    if (-1 == kiro_client_connect (client, argv[1], argv[2])) {
        kiro_client_free (client);
        return -1;
    }

    kiro_client_sync (client);
    kiro_trb_adopt (trb, kiro_client_get_memory (client));
    
    
    _Bool ok =
        init_app ("UCA Images", NULL, SDL_INIT_VIDEO) &&
        SDL_SetVideoMode (512, 512, 8, SDL_HWSURFACE);
    assert (ok);
    uint32_t mask = 0xffffffff;
    SDL_Surface *data_sf = SDL_CreateRGBSurfaceFrom (
                               kiro_trb_get_element (trb, 0), 512, 512, 8, 512,
                               mask, mask, mask, 0);
   

    SDL_Color colors[256];
    for (int i = 0; i < 256; i++) {
        colors[i].r = i;
        colors[i].g = i;
        colors[i].b = i;
    }
    SDL_SetPalette (data_sf, SDL_LOGPAL | SDL_PHYSPAL, colors, 0, 256);
    SDL_SetEventFilter (filter);
    
    
    int cont = 1;
    while (cont) {
        for (SDL_Event event; SDL_PollEvent (&event);)
            if (event.type == SDL_QUIT) cont = 0;

        if (kiro_client_sync (client) < 0) {
            g_warning ("Unable to get data from server. Stopping.");
            break;
        }
        SDL_Delay (10);
        render (data_sf);
    }

    kiro_client_free (client);
    kiro_trb_free (trb);
    return 0;
}