diff --git a/C/Server/main.c b/C/Server/main.c index a240af3..b6a5410 100644 --- a/C/Server/main.c +++ b/C/Server/main.c @@ -14,14 +14,16 @@ #define UDP_PORT 5005 // the port users will be connecting to #define DISCOVER_PORT 5006 // the port on which the server will broadcast its details -static void die(char *msg) -{ +#define MIN(X, Y) (((X) < (Y)) ? (X) : (Y)) + +#define MAX_PIXELS 140 + +static void die(char *msg) { fprintf(stderr, "%s\n", msg); exit(EXIT_FAILURE); } -static int get_udp_socket(int port) -{ +static int get_udp_socket(int port) { int sock_fd; struct sockaddr_in6 addr; @@ -41,8 +43,7 @@ static int get_udp_socket(int port) } -static ssize_t get_udp_packet(int sock_fd, char *packet, size_t size) -{ +static ssize_t get_udp_packet(int sock_fd, char *packet, size_t size) { ssize_t n_read; while ((n_read = recv(sock_fd, packet, size, 0)) < 0 ); return n_read; @@ -51,30 +52,27 @@ static ssize_t get_udp_packet(int sock_fd, char *packet, size_t size) // PixelVloed C version // application entry point -int main(int argc, char* argv[]) -{ - uint8_t packet[1120 + 32]; +int main(int argc, char* argv[]) { + uint8_t packet[ (8 * MAX_PIXELS) + 32]; // 8 bytes per pixel is the maximum size ssize_t packet_size; uint16_t x, y; uint8_t r, g, b, a; uint8_t pixellength = 0, protocol, version; uint16_t i; + uint8_t offset = 2; int sock_fd = get_udp_socket(UDP_PORT); - + // Init screen buffer if (!init_frame_buffer()) { die("Failed to init screen buffer.\n"); } - // draw... + // draw each udp packet while( (packet_size = get_udp_packet(sock_fd, (char*)&packet, sizeof(packet))) >= 0 ) { - - - // Get protocol from the packet - version = packet[1]; // - protocol = packet[0]; // Alpha in old + version = packet[1]; // the version + protocol = packet[0]; // protocol switch //printf("V:%d, P:%d\n", version, protocol); @@ -82,7 +80,8 @@ int main(int argc, char* argv[]) // Protocol 0: xyrgb 16:16:8:8:8 specified for each pixel case 0: pixellength = 7; - for (i=2; i>8; temp_r = temp_r + ((r * (a))>>8); temp_g = (temp_g * (256-a))>>8; @@ -128,18 +123,12 @@ void write_pixel_to_screen(uint16_t x, uint16_t y, uint8_t r, uint8_t g, uint8_t } // Write new pixels to buffer - if (vinfo.bits_per_pixel == 24) { - // 24bpp format + if (vinfo.bits_per_pixel == 24) { // 24bpp format pix_offset = 3 * x + y * finfo.line_length; - fbp[pix_offset+2] = temp_r; - fbp[pix_offset+1] = temp_g; - fbp[pix_offset+0] = temp_b; - } - if (vinfo.bits_per_pixel == 32) { - // 32bpp format + } else if (vinfo.bits_per_pixel == 32) { // 32bpp format pix_offset = 4 * x + y * finfo.line_length; - fbp[pix_offset+2] = temp_r; - fbp[pix_offset+1] = temp_g; - fbp[pix_offset+0] = temp_b; } + fbp[pix_offset+2] = temp_r; + fbp[pix_offset+1] = temp_g; + fbp[pix_offset+0] = temp_b; }