Development:Tutorials:SetupVideo

From DingooWiki

Jump to: navigation, search

Contents

[edit] Using SDL

As normal no need it any special setting, a simple example:

  • Note: 32bpp screen surfaces works, but 16bpp runs better.
 SDL_Surface * screen;
 
 screen = SDL_SetVideoMode(320, 240, 16, SDL_SWSURFACE);
 if (!screen) {
     fprintf(stderr, "Couldn't set video mode: %s\n", SDL_GetError());
         return 0;
 }

[edit] SDL Workarounds

To avoid the dingoo vsync errors:

  • Use SDL_Flip instead of SDL_UpdateRects.
  • With SDL_SetVideoMode I suggest to used SDL_SWSURFACE instead of SDL_HWSURFACE.

[edit] Using Kernel Framebuffer

Opening Framebuffer and setup a direct memory access (mmap):

  • Note: You need disable console output, more info below.
 int fb_dev;
 unsigned short *screen_base;
 
 fb_dev = open("/dev/fb0", O_RDWR);
 if(fb_dev)
 {
     screen_base = mmap(NULL, sizeof(unsigned short) * 320 * 240, PROT_READ | PROT_WRITE, MAP_SHARED, fb_dev, 0);
     return 1;
 }

Close Access:

 munmap(screen_base, sizeof(unsigned short) * 320 * 240);
 close(fb_dev);

Using the Framebuffer:

#define RGB16(r, g, b) (((r) << 11) | ((g) << 5) | (b))
#define RGB24(r, g, b) RGB16((r) >> 3, (g) >> 2, (b) >> 3)
 short color = RGB16(255, 0, 0);
 
 memset(screen_base, color, sizeof(unsigned short) * 320 * 240);

[edit] Disabling Console Output

If you use direct framebuffer access (NO SDL INIT), you need to disable console output:

 int con_dev = -1;
 int con_old_vt = -1;
 
 char vt_name[128];
 
 snprintf(vt_name, sizeof(vt_name), "%s%d", "/dev/tty", 1);
 int base_con_dev = open(vt_name, O_WRONLY);
 
 if (base_con_dev != -1) {
     int con_num;
 
     if (ioctl(base_con_dev, VT_OPENQRY, &con_num) != -1) {
         snprintf(vt_name, sizeof(vt_name), "%s%d", "/dev/tty", con_num);
         con_dev = open(vt_name, O_WRONLY);
 
         if (con_dev != -1) {
             struct vt_stat vts;
             if (ioctl(con_dev, VT_GETSTATE, &vts) == 0) con_old_vt = vts.v_active;
 
             if (ioctl(con_dev, VT_ACTIVATE, con_num) != -1
                 && ioctl(con_dev, VT_WAITACTIVE, con_num) != -1
                 && ioctl(con_dev, KDSETMODE, KD_GRAPHICS) != -1) goto con_success;
           }
     }
 
     if (con_dev != -1) {
         close(con_dev);
         con_dev = -1;
     }
 
     static const char cmd[] = "\033[?1;0;0c";
     write(base_con_dev, cmd, sizeof(cmd)-1);
 
 con_success:
     close(base_con_dev);
 }

To finally restore it, use the following:

 if (con_dev != -1) {
     ioctl(con_dev, KDSETMODE, KD_TEXT);
     if (con_old_vt != -1) ioctl(con_dev, VT_ACTIVATE, con_old_vt);
     close(con_dev);
     con_dev = -1;
 }