Ignore:
Timestamp:
Mar 8, 2006, 11:33:01 AM (14 years ago)
Author:
Sam Hocevar
Message:
  • Finished moving everything driver-specific to the apropriate driver_*.c files. Phew. Now they just need a few comments.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcaca/trunk/caca/driver_win32.c

    r549 r550  
    2323
    2424#include <windows.h>
     25
     26#include <stdlib.h>
    2527
    2628#include "caca.h"
     
    7375};
    7476
     77struct driver_private
     78{
     79    HANDLE hin, hout;
     80    HANDLE front, back;
     81    CHAR_INFO *buffer;
     82    CONSOLE_CURSOR_INFO cci;
     83};
     84
    7585static int win32_init_graphics(caca_t *kk)
    7686{
     
    7888    COORD size;
    7989
     90    kk->drv.p = malloc(sizeof(struct driver_private));
     91
    8092    /* This call is allowed to fail in case we already have a console */
    8193    AllocConsole();
    8294
    83     kk->win32.hin = GetStdHandle(STD_INPUT_HANDLE);
    84     kk->win32.hout = CreateFile("CONOUT$", GENERIC_READ | GENERIC_WRITE,
    85                                 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
    86                                 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    87     if(kk->win32.hout == INVALID_HANDLE_VALUE)
    88         return -1;
    89 
    90     GetConsoleCursorInfo(kk->win32.hout, &kk->win32.cci);
    91     kk->win32.cci.bVisible = FALSE;
    92     SetConsoleCursorInfo(kk->win32.hout, &kk->win32.cci);
    93 
    94     SetConsoleMode(kk->win32.hout, ENABLE_MOUSE_INPUT);
    95 
    96     kk->win32.front =
     95    kk->drv.p->hin = GetStdHandle(STD_INPUT_HANDLE);
     96    kk->drv.p->hout = CreateFile("CONOUT$", GENERIC_READ | GENERIC_WRITE,
     97                                 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
     98                                 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
     99    if(kk->drv.p->hout == INVALID_HANDLE_VALUE)
     100        return -1;
     101
     102    GetConsoleCursorInfo(kk->drv.p->hout, &kk->drv.p->cci);
     103    kk->drv.p->cci.bVisible = FALSE;
     104    SetConsoleCursorInfo(kk->drv.p->hout, &kk->drv.p->cci);
     105
     106    SetConsoleMode(kk->drv.p->hout, ENABLE_MOUSE_INPUT);
     107
     108    kk->drv.p->front =
    97109        CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE,
    98110                                  0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
    99     if(!kk->win32.front || kk->win32.front == INVALID_HANDLE_VALUE)
    100         return -1;
    101 
    102     kk->win32.back =
     111    if(!kk->drv.p->front || kk->drv.p->front == INVALID_HANDLE_VALUE)
     112        return -1;
     113
     114    kk->drv.p->back =
    103115        CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE,
    104116                                  0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
    105     if(!kk->win32.back || kk->win32.back == INVALID_HANDLE_VALUE)
    106         return -1;
    107 
    108     if(!GetConsoleScreenBufferInfo(kk->win32.hout, &csbi))
     117    if(!kk->drv.p->back || kk->drv.p->back == INVALID_HANDLE_VALUE)
     118        return -1;
     119
     120    if(!GetConsoleScreenBufferInfo(kk->drv.p->hout, &csbi))
    109121        return -1;
    110122
    111123    /* Sample code to get the biggest possible window */
    112     //size = GetLargestConsoleWindowSize(kk->win32.hout);
     124    //size = GetLargestConsoleWindowSize(kk->drv.p->hout);
    113125    cucul_set_size(kk->qq, csbi.srWindow.Right - csbi.srWindow.Left + 1,
    114126                           csbi.srWindow.Bottom - csbi.srWindow.Top + 1);
    115127    size.X = kk->qq->width;
    116128    size.Y = kk->qq->height;
    117     SetConsoleScreenBufferSize(kk->win32.front, size);
    118     SetConsoleScreenBufferSize(kk->win32.back, size);
    119 
    120     SetConsoleMode(kk->win32.front, 0);
    121     SetConsoleMode(kk->win32.back, 0);
    122 
    123     GetConsoleCursorInfo(kk->win32.front, &kk->win32.cci);
    124     kk->win32.cci.dwSize = 0;
    125     kk->win32.cci.bVisible = FALSE;
    126     SetConsoleCursorInfo(kk->win32.front, &kk->win32.cci);
    127     SetConsoleCursorInfo(kk->win32.back, &kk->win32.cci);
    128 
    129     SetConsoleActiveScreenBuffer(kk->win32.front);
    130 
    131     kk->win32.buffer = malloc(kk->qq->width * kk->qq->height
     129    SetConsoleScreenBufferSize(kk->drv.p->front, size);
     130    SetConsoleScreenBufferSize(kk->drv.p->back, size);
     131
     132    SetConsoleMode(kk->drv.p->front, 0);
     133    SetConsoleMode(kk->drv.p->back, 0);
     134
     135    GetConsoleCursorInfo(kk->drv.p->front, &kk->drv.p->cci);
     136    kk->drv.p->cci.dwSize = 0;
     137    kk->drv.p->cci.bVisible = FALSE;
     138    SetConsoleCursorInfo(kk->drv.p->front, &kk->drv.p->cci);
     139    SetConsoleCursorInfo(kk->drv.p->back, &kk->drv.p->cci);
     140
     141    SetConsoleActiveScreenBuffer(kk->drv.p->front);
     142
     143    kk->drv.p->buffer = malloc(kk->qq->width * kk->qq->height
    132144                               * sizeof(CHAR_INFO));
    133     if(kk->win32.buffer == NULL)
     145    if(kk->drv.p->buffer == NULL)
    134146        return -1;
    135147
     
    139151static int win32_end_graphics(caca_t *kk)
    140152{
    141     SetConsoleActiveScreenBuffer(kk->win32.hout);
    142     CloseHandle(kk->win32.back);
    143     CloseHandle(kk->win32.front);
    144 
    145     SetConsoleTextAttribute(kk->win32.hout, FOREGROUND_INTENSITY
     153    SetConsoleActiveScreenBuffer(kk->drv.p->hout);
     154    CloseHandle(kk->drv.p->back);
     155    CloseHandle(kk->drv.p->front);
     156
     157    SetConsoleTextAttribute(kk->drv.p->hout, FOREGROUND_INTENSITY
    146158                                             | FOREGROUND_RED
    147159                                             | FOREGROUND_GREEN
    148160                                             | FOREGROUND_BLUE);
    149     kk->win32.cci.bVisible = TRUE;
    150     SetConsoleCursorInfo(kk->win32.hout, &kk->win32.cci);
    151     CloseHandle(kk->win32.hout);
     161    kk->drv.p->cci.bVisible = TRUE;
     162    SetConsoleCursorInfo(kk->drv.p->hout, &kk->drv.p->cci);
     163    CloseHandle(kk->drv.p->hout);
     164
     165    free(kk->drv.p);
    152166
    153167    return 0;
     
    185199    for(i = 0; i < kk->qq->width * kk->qq->height; i++)
    186200    {
    187         kk->win32.buffer[i].Char.AsciiChar = kk->qq->chars[i] & 0x7f;
    188         kk->win32.buffer[i].Attributes =
     201        kk->drv.p->buffer[i].Char.AsciiChar = kk->qq->chars[i] & 0x7f;
     202        kk->drv.p->buffer[i].Attributes =
    189203                win32_fg_palette[kk->qq->attr[i] & 0xf]
    190204                 | win32_bg_palette[kk->qq->attr[i] >> 4];
     
    198212    rect.Right = kk->qq->width - 1;
    199213    rect.Bottom = kk->qq->height - 1;
    200     WriteConsoleOutput(kk->win32.front, kk->win32.buffer, size, pos, &rect);
     214    WriteConsoleOutput(kk->drv.p->front, kk->drv.p->buffer, size, pos, &rect);
    201215}
    202216
     
    216230    for( ; ; )
    217231    {
    218         GetNumberOfConsoleInputEvents(kk->win32.hin, &num);
     232        GetNumberOfConsoleInputEvents(kk->drv.p->hin, &num);
    219233        if(num == 0)
    220234            break;
    221235
    222         ReadConsoleInput(kk->win32.hin, &rec, 1, &num);
     236        ReadConsoleInput(kk->drv.p->hin, &rec, 1, &num);
    223237        if(rec.EventType == KEY_EVENT)
    224238        {
     
    289303void win32_init_driver(caca_t *kk)
    290304{
    291     kk->driver.driver = CACA_DRIVER_WIN32;
    292 
    293     kk->driver.init_graphics = win32_init_graphics;
    294     kk->driver.end_graphics = win32_end_graphics;
    295     kk->driver.set_window_title = win32_set_window_title;
    296     kk->driver.get_window_width = win32_get_window_width;
    297     kk->driver.get_window_height = win32_get_window_height;
    298     kk->driver.display = win32_display;
    299     kk->driver.handle_resize = win32_handle_resize;
    300     kk->driver.get_event = win32_get_event;
     305    kk->drv.driver = CACA_DRIVER_WIN32;
     306
     307    kk->drv.init_graphics = win32_init_graphics;
     308    kk->drv.end_graphics = win32_end_graphics;
     309    kk->drv.set_window_title = win32_set_window_title;
     310    kk->drv.get_window_width = win32_get_window_width;
     311    kk->drv.get_window_height = win32_get_window_height;
     312    kk->drv.display = win32_display;
     313    kk->drv.handle_resize = win32_handle_resize;
     314    kk->drv.get_event = win32_get_event;
    301315}
    302316
Note: See TracChangeset for help on using the changeset viewer.