Skip to content

Instantly share code, notes, and snippets.

@gucchan22
Last active August 29, 2015 14:14
Show Gist options
  • Save gucchan22/fb04e0e89ee3a5341925 to your computer and use it in GitHub Desktop.
Save gucchan22/fb04e0e89ee3a5341925 to your computer and use it in GitHub Desktop.
struct OAMData { int y, x, tile, flags; };
void render_line(int line) {
int x,c = 0;
struct OAMData oam_per_line[10];
unsigned int *screen_buff = sdl_get_framebuffer();
for(int i = 0; i < 40; i++) { // 40 * 4byte = 160byte (0xfe00 ~ )
int y = mem_get_raw(0xfe00 + (i * 4)) - 16;
if(line < y || line >= y + 8 + (sprite_size * 8)) continue;
oam_per_line[i].y = y;
oam_per_line[i].x = mem_get_raw(0xfe00 + (i * 4) + 1) - 8;
oam_per_line[i].tile = mem_get_raw(0xfe00 + (i * 4) + 2);
oam_per_line[i].flags = mem_get_raw(0xfe00 + (i * 4) + 3);
c++;
if(c == 10) break;
}
if(c) sort_sprites(oam_per_line, c);
for(int x = 0; x < 160; x++) {
if(line >= window_y && window_enable && line - window_y < 144) {
xm = x;
ym = line - window - y;
map_select = window_tilemap_select;
} else {
xm = (x + scroll_x) % 256;
ym = (line + scroll_y) % 256;
map_select = tilemap_select;
}
}
}
int lcd_cycle(void) {
int cycles = cpu_get_cycles();
int frame, prev_line;
if(sdl_update()) return 0;
frame = cycles % (70244 / 4); // HBlank, Reading OAM/VRAM, VBlank period.
lcd_line = frame / (456 / 4); // HBlank, Reading OAM/VRAM.
if(lcd_line != prev_line && lcd_line < 144) render_line(lcd_line);
if(prev_line == 143 && lcd_line == 144) {
draw_stuff();
interrupt(INTR_VBLANK);
sdl_frame();
}
prev_line = lcd_line;
return 1;
}
uint32_t rgb888[64] = {
0x787878, 0x2000B0, 0x2800B8, 0x6010A0, 0x982078, 0xB01030, 0xA03000, 0x784000,
0x485800, 0x386800, 0x386C00, 0x306040, 0x305080, 0x000000, 0x000000, 0x000000,
0xB0B0B0, 0x4060F8, 0x4040FF, 0x9040F0, 0xD840C0, 0xD84060, 0xE05000, 0xC07000,
0x888800, 0x50A000, 0x48A810, 0x48A068, 0x4090C0, 0x000000, 0x000000, 0x000000,
0xFFFFFF, 0x60A0FF, 0x5080FF, 0xA070FF, 0xF060FF, 0xFF60B0, 0xFF7830, 0xFFA000,
0xE8D020, 0x98E800, 0x70F040, 0x70E090, 0x60D0E0, 0x787878, 0x000000, 0x000000,
0xFFFFFF, 0x90D0FF, 0xA0B8FF, 0xC0B0FF, 0xE0B0FF, 0xFFB8E8, 0xFFC8B8, 0xFFD8A0,
0xFFF090, 0xC8F080, 0xA0F0A0, 0xA0FFC8, 0xA0FFF0, 0xA0A0A0, 0x000000, 0x000000
};
uint32_t col888[4] = { 0xbfd3a0, 0x86986c, 0x4a553b, 0x1f1f1f };
SDL_Window *window;
SDL_Renderer *renderer;
SDL_Texture *tex;
SDL_Init(SDL_INIT_EVERYTHING);
SDL_CreateWindowAndRenderer(GAMEBOY_SCREEN_HEIGHT, GAMEBOY_SCREEN_WIDTH, 0, &window, &renderer);
SDL_SetWindowTitle(window,"GucchanBoy (gucchan)");
SDL_RendererInfo info;
SDL_GetRendererInfo(renderer, &info);
tex = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGB888, SDL_TEXTUREACCESS_STREAMING, GAMEBOY_SCREEN_WIDTH, GAMEBOY_SCREEN_HEIGHT);
uint32_t* line;
uint8_t* line8;
int pitch;
SDL_LockTexture(tex, NULL, reinterpret_cast<void**>(&line8), &pitch);
for(int y=0;y<GAMEBOY_SCREEN_HEIGHT;y++){
line = reinterpret_cast<uint32_t*>(line8);
for(int x=0;x<GAMEBOY_SCREEN_WIDTH; x++) line[x] = col888[(x >> 4) & 3];
line8+=pitch;
}
SDL_UnlockTexture(tex);
SDL_RenderClear(renderer);
SDL_Rect rect;
rect.x = rect.y = 0;
rect.w = GAMEBOY_SCREEN_WIDTH;
rect.h = GAMEBOY_SCREEN_HEIGHT;
SDL_RenderCopy(renderer, tex, &rect, NULL);
SDL_RenderPresent(renderer);
SDL_Delay(300000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment