Tuesday, November 11, 2014

Simple modes

Some have expressed the interest for simple modes for the Bitbox. An recent commit is providing such mode , on top of the standard kernel.

The so-called simple modes are called like this because they provide a frame buffer interface, so the application programmer will only have to write bits and bytes to the video memory - much like it's done on PC. So the interface is a vram buch of memory and a color palette (because the tradeoff is a reduced palette or resolution)

There are two examples in the bitbox SDK (one of them below), but generally you'll need to define a simple makefile variable VGA_SIMPLE_MODE=X with the given mode and include the bitbox.mk standard makefile.

(Yes it's currently defined at compile time - if your program needs to switch between two modes at runtime, it might be time to switch to a standard kernel / engine - it's not difficult !)

The Modes

 The modes are the following:
mode type colors resolution VRAM kernel mode
0 text 2 80x252k+2k 640x480
1 text 2* 132x7510k+10k 800x600
2 graphical 2 800x60059k 800x600
3 graphical 4 640x40064k 640x480
4 graphical 16 400x30064k 800x600
5 graphical 256 320x20064k 640x480
10 text256*80x30<10k640x480
11 text256*120x75<10k800x600

*text modes 0 and 1 are only B&W (well, whatever you define as palette 0 and 1). Modes 10 and 11 use a 256 entry palette of u16 fg / u16 bg attributes. Beware that using mode 11 (really stretching the harwdare) with too much color changes per line might create dark lines.


All modes use a palette of u16. Set the color by setting a 0rrrrrgggggbbbbb 15- bit word entry in the palette array. By example, to assign a bright red color to color index 4 (zero-based), just set

extern uint16_t palette[16];
palette[4]=0b0111110000000000; //  binary literal or use RGB(x,y,z)

Default palettes

Text modes : 0,1

Text modes 0 and 1 define text modes with color attributes.
Mode 0 is 80 columns by 25 lines, Mode 1 is 132 columns by 75 lines.
Video Memory has one array of characters (vram)

Example to draw the ASCII table :
    #define SCREEN_W 132
    for (int i=0;i<256;i++)
      vram[(i/16)*SCREEN_W + i%16]=i;

Feel free to use memcpy / memset also (strcpy writes a zero character after the string)
    char *my_str = "Bitbox rules";
    memcpy(&vram[4*132+13], my_str,strlen(my_str));

Graphical modes : 2-5

Graphical mode map linearly pixels (color indexes) to bits in memory, ie to set a pixel as color 1 (defined in the palette) on coordinates 100,50 with mode 2, use
    extern uint32_t vram;
    // set a bit on the bit array of pixels, made of 32bit words
    vram[(50*800+100)/32] |= 1<<(100%32); 

or just use the draw_pixel or draw_line functions (of course to fill/copy a bunch of pixels, you'd better use memory access)

Example program 

The program can be found in the SDK examples. 

Here is it in its entirety: 




Simple, no ?

Simple Scrolling tilemap + sprites mode

Well, I cheated there is no such mode :) 
However, there is a simple example project using the engine that you can use very simply, just take the test_engine project in the SDK and tweak it !

No comments:

Post a Comment