Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SDL2/SDL.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <string.h>
- #include <fcntl.h>
- #include <sys/ioctl.h>
- #include <stdint.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <time.h>
- #include "doomdev.h"
- #include <errno.h>
- #define WIDTH 640
- #define HEIGHT 480
- #define COLOR(r, g, b) (((r)&0b11100000) | ((g)>>6<<3) | ((b) >> 5))
- #define FIXED_POINT(a, b) ((a<<16)|(b))
- // gcc -std=c11 -lSDL2
- int random(int min, int max)
- {
- return rand() % (max-min) + min;
- }
- void blit_texture(uint8_t* screen, SDL_Renderer* renderer, SDL_Color* colors)
- {
- SDL_Surface* surf = SDL_CreateRGBSurfaceFrom(screen, WIDTH, HEIGHT, 8, WIDTH, 0, 0, 0, 0);
- SDL_SetPaletteColors(surf->format->palette, colors, 0, 256);
- SDL_Texture* tex = SDL_CreateTextureFromSurface(renderer, surf);
- SDL_FreeSurface(surf);
- SDL_RenderCopy(renderer, tex, NULL, NULL);
- SDL_RenderPresent(renderer);
- SDL_DestroyTexture(tex);
- }
- void draw_line(int fd, int ax, int ay, int bx, int by, int color)
- {
- struct doomdev_line lines[1] = {
- { ax, ay, bx, by, color, 0}
- };
- struct doomdev_surf_ioctl_draw_lines params = {
- .lines_ptr = (uint64_t)lines,
- .lines_num = 1,
- };
- int res = ioctl(fd, DOOMDEV_SURF_IOCTL_DRAW_LINES, ¶ms);
- }
- void fill_rect(int fd, int ax, int ay, int width, int height, int color)
- {
- struct doomdev_fill_rect data[1] = {
- { ax, ay, width, height, color, 0}
- };
- struct doomdev_surf_ioctl_fill_rects params = {
- .rects_ptr = (uint64_t)data,
- .rects_num = 1,
- };
- int res = ioctl(fd, DOOMDEV_SURF_IOCTL_FILL_RECTS, ¶ms);
- }
- int create_surface(int fd, int width, int height)
- {
- struct doomdev_ioctl_create_surface param = {
- .width = width,
- .height = height,
- };
- int sfd = ioctl(fd, DOOMDEV_IOCTL_CREATE_SURFACE, ¶m);
- if (sfd < 0) {
- printf("doomdev create_surface fail\n");
- }
- return sfd;
- }
- int create_flat_tex(int fd, char* data)
- {
- struct doomdev_ioctl_create_flat param = {
- .data_ptr = (uint64_t)data
- };
- int sfd = ioctl(fd, DOOMDEV_IOCTL_CREATE_FLAT, ¶m);
- if (sfd < 0) {
- printf("doomdev create_flat_tex fail\n");
- }
- return sfd;
- }
- int create_texure(int fd, int size, int height, char* data)
- {
- struct doomdev_ioctl_create_texture param = {
- .data_ptr = (uint64_t)data,
- .size = size,
- .height = height
- };
- int sfd = ioctl(fd, DOOMDEV_IOCTL_CREATE_TEXTURE, ¶m);
- if (sfd < 0) {
- printf("doomdev create_tex fail\n");
- }
- return sfd;
- }
- int draw_background(int fd, int tex)
- {
- struct doomdev_surf_ioctl_draw_background params = {
- .flat_fd = tex
- };
- int res = ioctl(fd, DOOMDEV_SURF_IOCTL_DRAW_BACKGROUND, ¶ms);
- return res;
- }
- int copy_rect(int fd, int src, int ax, int ay, int destx, int desty, int width, int height)
- {
- struct doomdev_copy_rect data[1] = {
- { destx, desty, ax, ay, width, height}
- };
- struct doomdev_surf_ioctl_copy_rects params = {
- .rects_ptr = (uint64_t)data,
- .surf_src_fd = src,
- .rects_num = 1,
- };
- int res = ioctl(fd, DOOMDEV_SURF_IOCTL_COPY_RECTS, ¶ms);
- return res;
- }
- int draw_column(int fd, int flags, int texture, int translation, int colormap, int translation_idx, int offset, uint32_t ustart, uint32_t ustep, int x, int y1, int y2, int colormap_idx)
- {
- struct doomdev_column data[1] = {
- {
- .texture_offset = offset,
- .ustart = ustart,
- .ustep = ustep,
- .y1 = y1,
- .y2 = y2,
- .x = x,
- .colormap_idx = colormap_idx
- }
- };
- struct doomdev_surf_ioctl_draw_columns params = {
- .columns_ptr = (uint64_t)data,
- .texture_fd = texture,
- .translations_fd = translation,
- .colormaps_fd = colormap,
- .columns_num = 1,
- .draw_flags = flags,
- .translation_idx = translation_idx
- };
- return ioctl(fd, DOOMDEV_SURF_IOCTL_DRAW_COLUMNS, ¶ms);
- }
- void fetch_surf(uint8_t* buf, int fd)
- {
- pread(fd, buf, WIDTH * HEIGHT, 0);
- }
- int sdl_should_continue()
- {
- SDL_Event Events;
- while (SDL_PollEvent(&Events))
- {
- if (Events.type == SDL_QUIT)
- return 0;
- }
- return 1;
- }
- int main (int argc, char** argv)
- {
- uint8_t screen[WIDTH*HEIGHT];
- SDL_Event Events;
- srand(NULL);
- SDL_Window* window = NULL;
- window = SDL_CreateWindow
- (
- "", SDL_WINDOWPOS_UNDEFINED,
- SDL_WINDOWPOS_UNDEFINED,
- WIDTH,
- HEIGHT,
- SDL_WINDOW_SHOWN
- );
- // Setup renderer
- SDL_Renderer* renderer = NULL;
- renderer = SDL_CreateRenderer( window, -1, SDL_RENDERER_ACCELERATED);
- SDL_Color colors[256];
- for (int i = 0; i < 256; i++)
- {
- colors[i].r = i & 0b11100000;
- colors[i].g = (i & 0b00011000) << 3;
- colors[i].b = (i & 0b00000111) << 5;
- }
- // doom
- int doom_fd = open("/dev/doom0", O_RDWR);
- if (doom_fd < 0) {
- printf("error doom_fd: %d\n", errno);
- return -1;
- }
- int framebuffer = create_surface(doom_fd, WIDTH, HEIGHT);
- int surf = create_surface(doom_fd, WIDTH, HEIGHT);
- unsigned char image[64*64];
- for (int i = 0; i < 64*64; ++i)
- {
- int r = 0;//i*255/64/64;
- int g = 255;//*255/64/64;
- int b =0;
- image[i] = COLOR(r, g, b);
- }
- int tex = create_flat_tex(doom_fd, image);
- int tex2 = create_texure(doom_fd, 64*64, 64, image);
- while (sdl_should_continue())
- {
- //draw_background(surf, tex);
- //draw_line(surf, random(0, WIDTH-1), random(0, HEIGHT-1), random(0, WIDTH-1), random(0, HEIGHT-1), random(0, 255));
- int ax = random(0, WIDTH-2);
- int ay = random(0, HEIGHT-2);
- int w = random(0, WIDTH-1-ax);
- int h = random(0, HEIGHT-1-ay);
- //fill_rect(framebuffer, ax, ay, w, h, random(0, 255));
- for (int j = 0; j < 20; ++j)
- {
- draw_column(framebuffer, 0, tex2, 0, 0, 0, 0, FIXED_POINT(0, 0), FIXED_POINT(0, 1), 50+j, 0, 200, 0);
- }
- // sleep(1);
- // copy_rect(framebuffer, surf, 0, 0, 0, 0, WIDTH/2-1, HEIGHT-1);
- draw_line(framebuffer, 0, 0, 640-1, 480-1, 128);
- sleep(1);
- fetch_surf(screen, framebuffer);
- blit_texture(screen, renderer, colors);
- break;
- //usleep(6000); // 20
- }
- sleep(5);
- SDL_DestroyWindow(window);
- SDL_Quit();
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement