~kameliya/kyuubey

2c4029e1fc313f6a41060cd071076b42e1774b6a — Yuki Izumi 6 years ago 783e319
Mouse cursor acting like QBasic's aaaaaaa
6 files changed, 73 insertions(+), 21 deletions(-)

M main.c
A main.h
M qb.c
M qb.h
M renderer.c
M text.c
M main.c => main.c +44 -18
@@ 1,7 1,11 @@
#include "renderer.h"
#include "main.h"
#include "parser.h"
#include "qb.h"
#include "renderer.h"
#include "text.h"
#include "parser.h"

int mouse_x = 0;
int mouse_y = 0;

static void loop(void);



@@ 55,24 59,46 @@ void loop(void) {

        while (SDL_PollEvent(&event)) {
            switch (event.type) {
                case SDL_QUIT:
                    return;

                case SDL_KEYDOWN:
                    if (event.key.repeat) {
                        break;
                    }

                    qb_keypress(event.key.keysym.sym, event.key.keysym.mod);
                    keydown_tick = SDL_GetTicks();
                    keydown_sym = event.key.keysym.sym;
                    keydown_mod = event.key.keysym.mod;
                    typematic_on = 0;
                    break;
            case SDL_QUIT:
                return;

                case SDL_KEYUP:
                    keydown_tick = 0;
            case SDL_KEYDOWN:
                if (event.key.repeat) {
                    break;
                }

                qb_keypress(event.key.keysym.sym, event.key.keysym.mod);
                keydown_tick = SDL_GetTicks();
                keydown_sym = event.key.keysym.sym;
                keydown_mod = event.key.keysym.mod;
                typematic_on = 0;
                break;

            case SDL_KEYUP:
                keydown_tick = 0;
                break;

            case SDL_MOUSEMOTION: {
                int old_x = mouse_x,
                    old_y = mouse_y;
                mouse_x = event.motion.x;
                mouse_y = event.motion.y;

                if (mouse_x != old_x || mouse_y != old_y) {
                    text_refresh();
                }
                break;
            }

            case SDL_MOUSEBUTTONDOWN:
                mouse_x = event.button.x;
                mouse_y = event.button.y;
                text_refresh();
                if (event.button.button == SDL_BUTTON_LEFT ||
                        event.button.button == SDL_BUTTON_RIGHT) {
                    qb_mouseclick(event.button.button == SDL_BUTTON_LEFT ?  1 : 2);
                }
                break;
            }
        }


A main.h => main.h +9 -0
@@ 0,0 1,9 @@
#ifndef MAIN_H
#define MAIN_H

extern int mouse_x;
extern int mouse_y;

#endif

/* vim: set sw=4 et: */

M qb.c => qb.c +6 -0
@@ 2,6 2,7 @@
#include <stdlib.h>
#include <string.h>

#include "main.h"
#include "qb.h"
#include "text.h"



@@ 461,4 462,9 @@ void qb_keypress(SDL_Keycode sym, Uint16 mod) {
    text_refresh();
}

void qb_mouseclick(int button) {
    render();
    text_refresh();
}

/* vim: set sw=4 et: */

M qb.h => qb.h +1 -0
@@ 7,6 7,7 @@ extern int qb_running;

void qb_init(void);
void qb_keypress(SDL_Keycode sym, Uint16 mod);
void qb_mouseclick(int button);

#endif


M renderer.c => renderer.c +2 -0
@@ 38,6 38,8 @@ int renderer_init(void) {

    sfont = read_raw_sdlfont("cp437.vga");

    SDL_ShowCursor(SDL_DISABLE);

    return 0;
}


M text.c => text.c +11 -3
@@ 1,5 1,6 @@
#include "text.h"
#include "main.h"
#include "renderer.h"
#include "text.h"

unsigned short screen[80 * 25];
int screen_cursor_x;


@@ 12,7 13,7 @@ void text_refresh(void) {

    for (int y = 0; y < 25; ++y) {
        for (int x = 0; x < 80; ++x) {
            render_sfont(sfont, screen[y * 80 + x], x, y);
            text_refresh_at(x, y);
        }
    }



@@ 24,7 25,14 @@ void text_refresh(void) {
}

void text_refresh_at(int x, int y) {
    render_sfont(sfont, screen[y * 80 + x], x, y);
    unsigned short pair = screen[y * 80 + x];
    if (mouse_x / FONT_WIDTH == x && mouse_y / FONT_HEIGHT == y) {
        pair =
            ((7 - (pair >> 12)) << 12) |
            ((7 - ((pair >> 8) & 0xF)) << 8) |
            (pair & 0xFF);
    }
    render_sfont(sfont, pair, x, y);
}

void text_draw_cursor(int x, int y) {