post
poster: psYchotic
description: Display binary in 2D with SDL
language: C
[download]
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*******************************************************************************
 * The MIT License
 * 
 * Copyright (c) <2009> Stefan 'psYchotic' Zwanenburg
 *                      stefanhetzwaantje@gmail.com
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * Compile with:
 *      gcc display_data.c `sdl-config --cflags --libs`
 ******************************************************************************/

#include "SDL.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Size of each rectangle
#define RECTSIZE 2

short paused = 0;

// Handles events.
int event_filter(const SDL_Event *event) {
    // User defined event to push when
    // we unpause
    SDL_Event pause_event;
    pause_event.type = SDL_USEREVENT;
    pause_event.user.code = 0;
    pause_event.user.data1 = NULL;
    pause_event.user.data2 = NULL;

    // What kind of event is being processed?
    switch (event->type) {
        // A key has been pressed
        case SDL_KEYDOWN:
            switch (event->key.keysym.sym) {
                case SDLK_q:
                case SDLK_ESCAPE:
                    printf("Quitting because of a keypress\n");
                    exit(EXIT_SUCCESS);
                    break;
                case SDLK_SPACE:
                    // Pause and send an event.
                    paused ^= 1;
                    SDL_PushEvent(&pause_event);
                    break;
                default:
                    break;
            }
            break;
        default:
            break;
    }

    return 0;
}

int main(int argc, char **argv) {
    // Initialize SDL.
    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE | SDL_INIT_EVENTTHREAD)) {
        printf("Couldn't initialize SDL: %s.\nQuitting.\n", SDL_GetError());
        exit(EXIT_FAILURE);
    }

    // Make sure SDL_Quit is called upon exiting this program.
    atexit(SDL_Quit);

    SDL_Surface *screen;
    int video_mask = SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_ANYFORMAT;
    int i;
    // Loop for processing the commandline arguments.
    for (i = 1; i < argc; i++) {
        if (!strcmp("-fs", argv[i]) || !strcmp("--fullscreen", argv[i])) {
            video_mask |= SDL_FULLSCREEN;
        } else if (!strcmp("-h", argv[i]) || !strcmp("--help", argv[i])) {
            printf("Usage: %s [-h | --help] [-fs | --fullscreen]\n", argv[0]);
            exit(EXIT_SUCCESS);
        }
    }

    // Sets up a surface to draw on.
    if ((video_mask & SDL_FULLSCREEN) == SDL_FULLSCREEN) {
        screen = SDL_SetVideoMode(0, 0, 32, video_mask);
    } else {
        screen = SDL_SetVideoMode(640, 480, 32, video_mask);
    }

    if (!screen) {
        printf("Couldn't create a surface for SDL: %s.\nQuitting\n", SDL_GetError());
        exit(EXIT_FAILURE);
    }

    // Don't show a mouse cursor
    SDL_ShowCursor(SDL_DISABLE);
    // Set our event filter.
    SDL_SetEventFilter(event_filter);
    SDL_Rect rect;
    SDL_Event event;
    // Standard input.
    SDL_RWops *input = SDL_RWFromFP(stdin, 0);
    if (!input) {
        printf("Failed to open stdin. Quitting.\n");
        exit(EXIT_FAILURE);
    }
    short filled = 0;
    unsigned char buf[3];

    while (1) {
        // If paused, wait for an event.
        while (paused && SDL_WaitEvent(&event)) {}

        // Read 3 bytes (RGB) from standard input
        if (SDL_RWread(input, buf, 1, 3) != 3) {
            printf("Quitting because there is no more input\n");
            exit(EXIT_SUCCESS);
            continue;
        }

        // If a full horizontal line has been drawn,
        // shift the whole surface up and clear the bottom
        // line.
        if (filled >= screen->w) {
            filled = 0;
            rect.x = 0;         rect.y = RECTSIZE;
            rect.w = screen->w; rect.h = screen->h - RECTSIZE;
            SDL_BlitSurface(screen, &rect, screen, NULL);
            rect.y = screen->h - RECTSIZE;
            rect.h = RECTSIZE;
            SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 0x00, 0x00, 0x00));
            SDL_Flip(screen);
        }

        // Draw a single rectangle
        rect.x = filled;    rect.y = screen->h - RECTSIZE;
        rect.w = RECTSIZE;   rect.h = RECTSIZE;
        SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, buf[0], buf[1], buf[2]));
        filled += RECTSIZE;
    }

    return 0;
}