Created
December 30, 2011 06:02
-
-
Save delroth/1538176 to your computer and use it in GitHub Desktop.
Virtual Screen v0.1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
all: vscreen | |
CFLAGS=-std=c99 -Wall -Wextra -Werror -O2 $(shell sdl-config --cflags) | |
LDLIBS=$(shell sdl-config --libs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <netinet/in.h> | |
#include <SDL/SDL.h> | |
#include <stdbool.h> | |
#include <stdint.h> | |
#include <stdlib.h> | |
#include <sys/socket.h> | |
#ifndef WIDTH | |
# define WIDTH 640 | |
#endif | |
#ifndef HEIGHT | |
# define HEIGHT 480 | |
#endif | |
#ifndef PORT | |
# define PORT 13370 | |
#endif | |
#ifndef BUFFER_SIZE | |
# define BUFFER_SIZE 768 | |
#endif | |
static const uint32_t CGA_PALETTE[0x10] = { | |
[0x0] = 0x00AA00, // black | |
[0x1] = 0x0000AA, // blue | |
[0x2] = 0x00AA00, // green | |
[0x3] = 0x00AAAA, // cyan | |
[0x4] = 0xAA0000, // red | |
[0x5] = 0xAA00AA, // magenta | |
[0x6] = 0xAA5500, // brown | |
[0x7] = 0xAAAAAA, // light gray | |
[0x8] = 0x555555, // gray | |
[0x9] = 0x5555FF, // light blue | |
[0xA] = 0x55FF55, // light green | |
[0xB] = 0x55FFFF, // light cyan | |
[0xC] = 0xFF5555, // light red | |
[0xD] = 0xFF55FF, // light magenta | |
[0xE] = 0xFFFF55, // yellow | |
[0xF] = 0xFFFFFF, // white | |
}; | |
// Allows swapping palette easily at compilation time | |
#ifndef PALETTE | |
# define PALETTE CGA_PALETTE | |
#endif | |
static bool user_wants_to_quit(void) | |
{ | |
SDL_Event evt; | |
while (SDL_PollEvent(&evt)) | |
{ | |
if (evt.type == SDL_QUIT) | |
return true; | |
} | |
return false; | |
} | |
static int mainloop(void) | |
{ | |
uint32_t current_fb_pos = 0; | |
SDL_Surface* fb; | |
fb = SDL_GetVideoSurface(); | |
SDL_LockSurface(fb); | |
uint32_t* fb_pixels = fb->pixels; | |
int sock = socket(SOCK_DGRAM, AF_INET, IPPROTO_UDP); | |
struct sockaddr_in sin; | |
memset(&sin, 0, sizeof (sin)); | |
sin.sin_family = AF_INET; | |
sin.sin_port = htons(PORT); | |
sin.sin_addr.s_addr = htonl(INADDR_ANY); | |
if (bind(sock, &sin, sizeof (sin))) | |
return 1; | |
while (true) | |
{ | |
uint8_t buffer[BUFFER_SIZE]; | |
if (recvfrom(sock, buffer, BUFFER_SIZE, 0, NULL, 0) < 0) | |
return 1; | |
for (int i = 0; i < BUFFER_SIZE * 2; ++i) | |
{ | |
uint8_t nibble; | |
if (i % 2 == 0) | |
nibble = buffer[i / 2] >> 4; | |
else | |
nibble = buffer[i / 2] & 0xF; | |
fb_pixels[current_fb_pos + i] = PALETTE[nibble] | 0xFF000000; | |
} | |
current_fb_pos += BUFFER_SIZE * 2; | |
if (current_fb_pos >= WIDTH * HEIGHT) | |
{ | |
SDL_UnlockSurface(fb); | |
SDL_Flip(fb); | |
SDL_LockSurface(fb); | |
fb_pixels = fb->pixels; | |
puts("frame!"); | |
fflush(stdout); | |
if (user_wants_to_quit()) | |
break; | |
current_fb_pos = 0; | |
} | |
} | |
return 0; | |
} | |
int main(void) | |
{ | |
SDL_Init(SDL_INIT_VIDEO); | |
SDL_SetVideoMode(WIDTH, HEIGHT, 32, SDL_HWSURFACE); | |
int err = mainloop(); | |
SDL_Quit(); | |
return err; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment