bluh
mew
This commit is contained in:
parent
636dabd8b1
commit
a8b50a2e94
3
.vscode/settings.json
vendored
3
.vscode/settings.json
vendored
|
@ -50,6 +50,7 @@
|
|||
"stdexcept": "cpp",
|
||||
"streambuf": "cpp",
|
||||
"typeinfo": "cpp",
|
||||
"execution": "cpp"
|
||||
"execution": "cpp",
|
||||
"stdbool.h": "c"
|
||||
}
|
||||
}
|
158
source/io/ctrl.c
158
source/io/ctrl.c
|
@ -24,8 +24,10 @@ struct InputRecord {
|
|||
|
||||
union {
|
||||
struct {
|
||||
u32 value;
|
||||
bool is_down;
|
||||
} key;
|
||||
struct {
|
||||
u64 value;
|
||||
} axis;
|
||||
struct {
|
||||
u32 x;
|
||||
|
@ -36,16 +38,31 @@ struct InputRecord {
|
|||
double timestamp;
|
||||
};
|
||||
|
||||
struct InputResult {
|
||||
struct InputBuffer {
|
||||
struct InputRecord records[IO_BUF_SIZE];
|
||||
size_t count;
|
||||
struct InputRecord buf[IO_BUF_SIZE];
|
||||
pthread_mutex_t mutex;
|
||||
};
|
||||
|
||||
struct InputBuffer NewInputBuffer()
|
||||
{
|
||||
struct InputBuffer buf;
|
||||
buf.count = 0;
|
||||
buf.mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
return buf;
|
||||
}
|
||||
|
||||
struct Axis {
|
||||
u8 type;
|
||||
|
||||
union {
|
||||
struct {
|
||||
u32 value;
|
||||
bool is_down;
|
||||
u32 is_down : 1;
|
||||
u32 is_up : 1;
|
||||
} key;
|
||||
struct {
|
||||
u64 value;
|
||||
} axis;
|
||||
struct {
|
||||
u32 x;
|
||||
|
@ -104,87 +121,126 @@ hashtype hash_ident(u16f id, enum InputType type)
|
|||
return Hash(&obj, sizeof(struct ident));
|
||||
}
|
||||
|
||||
struct ctrl_bkt {
|
||||
hashtype bind_hash;
|
||||
size_t index;
|
||||
hashtype code_hash;
|
||||
|
||||
struct code_bkt {
|
||||
hashtype hash;
|
||||
u16 code;
|
||||
struct Axis axis;
|
||||
};
|
||||
|
||||
struct bind_bkt {
|
||||
hashtype hash;
|
||||
u16 bind;
|
||||
struct Axis *axis;
|
||||
};
|
||||
|
||||
struct Ctrl {
|
||||
size_t capacity;
|
||||
size_t filled;
|
||||
struct ctrl_bkt *bkts;
|
||||
struct {
|
||||
size_t capacity;
|
||||
size_t filled;
|
||||
struct code_bkt *bkts;
|
||||
} codes;
|
||||
|
||||
struct {
|
||||
size_t capacity;
|
||||
size_t filled;
|
||||
struct bind_bkt *bkts;
|
||||
} binds;
|
||||
|
||||
pthread_mutex_t mutex;
|
||||
};
|
||||
typedef struct Ctrl Ctrl;
|
||||
|
||||
Ctrl NewCtrl(struct ctrl_bkt *bkts_prealloc, size_t capacity)
|
||||
Ctrl NewCtrl(struct code_bkt *codes, size_t c, struct bind_bkt *binds, size_t b)
|
||||
{
|
||||
memset(codes, 0, sizeof(struct code_bkt) * c);
|
||||
memset(binds, 0, sizeof(struct bind_bkt) * b);
|
||||
|
||||
Ctrl ctrl;
|
||||
ctrl.capacity = capacity;
|
||||
ctrl.filled = 0;
|
||||
memset(bkts_prealloc, 0, sizeof(struct ctrl_bkt) * capacity);
|
||||
ctrl.bkts = bkts_prealloc;
|
||||
ctrl.codes.bkts = codes;
|
||||
ctrl.binds.bkts = binds;
|
||||
ctrl.mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
|
||||
return ctrl;
|
||||
}
|
||||
|
||||
struct ctrl_bkt *get_bkt(Ctrl *ctrl, size_t i)
|
||||
void CtrlPoll(struct InputBuffer *buf)
|
||||
{
|
||||
return &ctrl->bkts[i];
|
||||
for (size_t i = 0; i < buf->count; i++) {
|
||||
struct InputRecord *rec = &buf->records[i];
|
||||
|
||||
switch (rec->type) {
|
||||
case KEY:
|
||||
key_update();
|
||||
break;
|
||||
case AXIS:
|
||||
axis_update();
|
||||
break;
|
||||
case JOYSTICK:
|
||||
joystick_update();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct ctrl_bkt *find_bind(Ctrl *ctrl, hashtype bind_hash, hashtype search)
|
||||
struct ctrl_bkt *get_code_bkt(Ctrl *ctrl, size_t i)
|
||||
{
|
||||
size_t i = bind_hash % ctrl->capacity;
|
||||
assert(i < ctrl->codes.capacity);
|
||||
return &ctrl->codes.bkts[i];
|
||||
}
|
||||
|
||||
struct ctrl_bkt *next;
|
||||
for (size_t offset = 0; offset < ctrl->capacity; offset++) {
|
||||
i = (i + 1) % ctrl->capacity;
|
||||
struct ctrl_bkt *get_bind_bkt(Ctrl *ctrl, size_t i)
|
||||
{
|
||||
assert(i < ctrl->binds.capacity);
|
||||
return &ctrl->binds.bkts[i];
|
||||
}
|
||||
|
||||
next = get_bkt(ctrl, i);
|
||||
if (next->bind_hash != search)
|
||||
continue;
|
||||
struct code_bkt *find_code(Ctrl *ctrl, hashtype hash)
|
||||
{
|
||||
const size_t index = hash % ctrl->codes.capacity;
|
||||
size_t i = index;
|
||||
|
||||
return next;
|
||||
while (i != index - 1) {
|
||||
struct code_bkt *bkt = get_code_bkt(ctrl, i);
|
||||
if (bkt->hash == 0)
|
||||
return bkt;
|
||||
|
||||
i = (i + 1) % ctrl->codes.capacity;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
struct ctrl_bkt *find_code(Ctrl *ctrl, size_t *i, hashtype search)
|
||||
struct ctrl_bkt *find_bind(Ctrl *ctrl, hashtype hash)
|
||||
{
|
||||
struct ctrl_bkt *next;
|
||||
for (size_t offset = 0; offset < ctrl->capacity; offset++) {
|
||||
*i = (*i + 1) % ctrl->capacity;
|
||||
const size_t index = hash % ctrl->binds.capacity;
|
||||
size_t i = index;
|
||||
|
||||
next = get_bkt(ctrl, *i);
|
||||
if (next->code_hash != search)
|
||||
continue;
|
||||
while (i != index - 1) {
|
||||
struct bind_bkt *bkt = get_bind_bkt(ctrl, i);
|
||||
if (bkt->hash == 0)
|
||||
return bkt;
|
||||
|
||||
return next;
|
||||
i = (i + 1) % ctrl->binds.capacity;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool CtrlMap(Ctrl *ctrl, u16f bind, u16f code, enum InputType type)
|
||||
{
|
||||
if (ctrl->filled == ctrl->capacity)
|
||||
if (ctrl->binds.filled == ctrl->binds.capacity)
|
||||
return false;
|
||||
|
||||
hashtype bind_hash = hash_ident(bind, type);
|
||||
struct ctrl_bkt *bind_bkt = find_bind(ctrl, bind_hash, 0);
|
||||
bind_bkt->bind_hash = bind_hash;
|
||||
|
||||
hashtype code_hash = hash_ident(code, type);
|
||||
size_t code_i = code_hash % ctrl->capacity;
|
||||
struct ctrl_bkt *code_bkt = find_code(ctrl, &code_i, 0);
|
||||
code_bkt->code_hash = code_hash;
|
||||
struct code_bkt *code_bkt = find_code(ctrl, code_hash);
|
||||
code_bkt->hash = code_hash;
|
||||
code_bkt->code = code;
|
||||
|
||||
bind_bkt->index = code_i;
|
||||
ctrl->filled += 1;
|
||||
hashtype bind_hash = hash_ident(bind, type);
|
||||
struct bind_bkt *bind_bkt = find_bind(ctrl, bind_hash);
|
||||
bind_bkt->hash = bind_hash;
|
||||
bind_bkt->bind = bind;
|
||||
bind_bkt->axis = &code_bkt->axis;
|
||||
|
||||
ctrl->binds.filled += 1;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -199,11 +255,9 @@ struct Axis *find_bind_axis(Ctrl *ctrl, u16f bind, enum InputType type)
|
|||
return &get_bkt(ctrl, bind_bkt->index)->axis;
|
||||
}
|
||||
|
||||
struct Axis *CtrlGet(Ctrl *ctrl, u16f code, enum InputType type)
|
||||
struct Axis *CtrlGet(Ctrl *ctrl, u16f code, u8f type)
|
||||
{
|
||||
hashtype code_hash = hash_ident(code, type);
|
||||
size_t code_i = code_hash % ctrl->capacity;
|
||||
struct ctrl_bkt *code_bkt = find_code(ctrl, &code_i, code_hash);
|
||||
= struct ctrl_bkt *code_bkt = find_code(ctrl, code, type);
|
||||
if (code_bkt == nullptr)
|
||||
return nullptr;
|
||||
|
||||
|
@ -216,12 +270,12 @@ bool CtrlUpdateKey(Ctrl *ctrl, struct InputRecord *record)
|
|||
if (axis == nullptr)
|
||||
return false;
|
||||
|
||||
if (record->data.axis.is_down) {
|
||||
if (record->data.key.is_down) {
|
||||
axis->last_pressed = record->timestamp;
|
||||
} else {
|
||||
axis->last_released = record->timestamp;
|
||||
}
|
||||
axis->data.axis.is_down = record->data.axis.is_down;
|
||||
axis->data.axis.value = record->data.key.is_down;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -25,8 +25,10 @@ struct InputRecord {
|
|||
|
||||
union {
|
||||
struct {
|
||||
u32 value;
|
||||
bool is_down;
|
||||
} key;
|
||||
struct {
|
||||
u64 value;
|
||||
} axis;
|
||||
struct {
|
||||
u32 x;
|
||||
|
@ -37,16 +39,23 @@ struct InputRecord {
|
|||
double timestamp;
|
||||
};
|
||||
|
||||
struct InputResult {
|
||||
struct InputBuffer {
|
||||
struct InputRecord records[IO_BUF_SIZE];
|
||||
size_t count;
|
||||
struct InputRecord buf[IO_BUF_SIZE];
|
||||
pthread_mutex_t mutex;
|
||||
};
|
||||
|
||||
struct InputBuffer NewInputBuffer();
|
||||
|
||||
struct Axis {
|
||||
union {
|
||||
struct {
|
||||
u32 value;
|
||||
bool is_down;
|
||||
u32 is_down : 1;
|
||||
u32 is_up : 1;
|
||||
} key;
|
||||
struct {
|
||||
u64 value;
|
||||
} axis;
|
||||
struct {
|
||||
u32 x;
|
||||
|
@ -83,8 +92,11 @@ typedef u32 hashtype;
|
|||
|
||||
struct ctrl_bkt {
|
||||
hashtype bind_hash;
|
||||
u16 bind;
|
||||
size_t index;
|
||||
|
||||
hashtype code_hash;
|
||||
u16 code;
|
||||
|
||||
struct Axis axis;
|
||||
};
|
||||
|
|
|
@ -7,67 +7,35 @@
|
|||
|
||||
#include "ctrl.h"
|
||||
#include "fumotris.h"
|
||||
#include "gametime.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
#include "win.h"
|
||||
#endif
|
||||
|
||||
bool dispatch(Ctrl *ctrl, struct InputRecord *record)
|
||||
#define IO_BUF_SIZE 8
|
||||
|
||||
struct input_args {
|
||||
Ctrl *ctrl;
|
||||
struct InputBuffer *in_buf;
|
||||
};
|
||||
|
||||
void *block_input(void *args_ptr)
|
||||
{
|
||||
switch (record->type) {
|
||||
case KEY:
|
||||
return CtrlUpdateKey(ctrl, record);
|
||||
case AXIS:
|
||||
return CtrlUpdateAxis(ctrl, record);
|
||||
case JOYSTICK:
|
||||
return CtrlUpdateJoystick(ctrl, record);
|
||||
case WINDOW:
|
||||
return CtrlUpdateWindow(ctrl, record);
|
||||
case ESCAPE:
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool write_result(Ctrl *ctrl, struct InputResult *result)
|
||||
{
|
||||
double now = GetTime();
|
||||
pthread_mutex_lock(&ctrl->mutex);
|
||||
|
||||
for (size_t i = 0; i < result->count; i++) {
|
||||
if (result->buf[i].type == ESCAPE)
|
||||
return false;
|
||||
result->buf[i].timestamp = now;
|
||||
|
||||
dispatch(ctrl, &result->buf[i]);
|
||||
}
|
||||
|
||||
pthread_mutex_unlock(&ctrl->mutex);
|
||||
return true;
|
||||
}
|
||||
|
||||
void *block_input(void *args)
|
||||
{
|
||||
Ctrl *ctrl = args;
|
||||
struct InputResult result;
|
||||
struct input_args *args = args_ptr;
|
||||
Ctrl *ctrl = args->ctrl;
|
||||
struct InputBuffer *in_buf = args->in_buf;
|
||||
|
||||
input_loop:
|
||||
bool success;
|
||||
|
||||
#ifdef _WIN32
|
||||
success = WindowsBlockInput(&result);
|
||||
success = WindowsBlockInput(&in_buf);
|
||||
#endif
|
||||
|
||||
if (!success) {
|
||||
if (!success)
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (!write_result(ctrl, &result)) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
goto input_loop;
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
|
|
@ -8,4 +8,10 @@
|
|||
#include "ctrl.h"
|
||||
#include "fumotris.h"
|
||||
|
||||
struct InputBuffer {
|
||||
struct InputRecord records[IO_BUF_SIZE];
|
||||
size_t count;
|
||||
pthread_mutex_t mutex;
|
||||
};
|
||||
|
||||
void StartInput(Ctrl *ctrl);
|
|
@ -21,9 +21,9 @@ bool WindowsInit()
|
|||
return true;
|
||||
}
|
||||
|
||||
bool WindowsBlockInput(struct InputResult *result)
|
||||
bool WindowsBlockInput(struct InputBuffer *buf)
|
||||
{
|
||||
return WinBlockInput(result);
|
||||
return WinBlockInput(buf);
|
||||
}
|
||||
|
||||
bool WindowsWait(double seconds)
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
#pragma once
|
||||
#include <pthread.h>
|
||||
#include <iso646.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
@ -9,6 +10,6 @@
|
|||
|
||||
bool WindowsInit();
|
||||
|
||||
bool WindowsBlockInput(struct InputResult *result);
|
||||
bool WindowsBlockInput(struct InputBuffer *buf);
|
||||
|
||||
bool WindowsWait(double seconds);
|
|
@ -7,6 +7,7 @@
|
|||
#include <windows.h>
|
||||
|
||||
#include "fumotris.h"
|
||||
#include "gametime.h"
|
||||
#include "input.h"
|
||||
|
||||
struct Windows {
|
||||
|
@ -45,7 +46,7 @@ void set_key_record(struct InputRecord *record, KEY_EVENT_RECORD win_key)
|
|||
{
|
||||
record->type = KEY;
|
||||
record->id = win_key.wVirtualKeyCode;
|
||||
record->data.axis.is_down = win_key.bKeyDown;
|
||||
record->data.key.is_down = win_key.bKeyDown;
|
||||
|
||||
if (win_key.wVirtualKeyCode == VK_ESCAPE)
|
||||
record->type = ESCAPE;
|
||||
|
@ -100,27 +101,31 @@ bool dispatch_record(struct InputRecord *record, INPUT_RECORD win_record)
|
|||
return true;
|
||||
}
|
||||
|
||||
bool WinBlockInput(struct InputResult *result)
|
||||
bool WinBlockInput(struct InputBuffer *buf)
|
||||
{
|
||||
INPUT_RECORD buf[8];
|
||||
size_t win_size = IO_BUF_SIZE - buf->count;
|
||||
INPUT_RECORD win_buf[win_size];
|
||||
DWORD count;
|
||||
|
||||
if (!ReadConsoleInput(windows.input_handle, buf, 8, &count))
|
||||
if (!ReadConsoleInput(windows.input_handle, win_buf, win_size, &count))
|
||||
return false;
|
||||
|
||||
size_t unused_offset = 0;
|
||||
double now = GetTime();
|
||||
pthread_mutex_lock(&buf->mutex);
|
||||
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
struct InputRecord record;
|
||||
record.timestamp = now;
|
||||
|
||||
bool include = dispatch_record(&record, buf[i]);
|
||||
if (record.type == ESCAPE)
|
||||
return false;
|
||||
bool include = dispatch_record(&record, win_buf[i]);
|
||||
if (!include)
|
||||
unused_offset += 1;
|
||||
continue;
|
||||
|
||||
result->buf[i - unused_offset] = record;
|
||||
buf->records[buf->count] = record;
|
||||
buf->count += 1;
|
||||
}
|
||||
result->count = count - unused_offset;
|
||||
|
||||
pthread_mutex_unlock(&buf->mutex);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -13,6 +13,6 @@ bool WinInitTimer();
|
|||
|
||||
bool WinInitConsole();
|
||||
|
||||
bool WinBlockInput(struct InputResult *result);
|
||||
bool WinBlockInput(struct InputBuffer *buf);
|
||||
|
||||
bool WinWait(double seconds);
|
|
@ -73,39 +73,13 @@ void Loop(Ctrl *ctrl)
|
|||
falling.blks = falling_blks;
|
||||
|
||||
for (int i = 0; i < 7779997; i++) {
|
||||
|
||||
|
||||
TetrMapToTermBuf(&board, &term);
|
||||
TetrMapToTermBuf(&falling, &term);
|
||||
|
||||
size_t size = TermBufToChars(&term, out, out_max);
|
||||
//puts(out);
|
||||
puts("\x1b[H");
|
||||
|
||||
//falling.y += 1;
|
||||
|
||||
for(int j = 0; j < ctrl->capacity; j++) {
|
||||
printf("val:%u, is_down:%u, x:%u, y:%u, press:%f, relse:%f\n",
|
||||
ctrl->bkts[j].axis.data.axis.value, ctrl->bkts[j].axis.data.axis.is_down,
|
||||
ctrl->bkts[j].axis.data.joystick.x, ctrl->bkts[j].axis.data.joystick.y,
|
||||
ctrl->bkts[j].axis.last_pressed, ctrl->bkts[j].axis.last_released);
|
||||
}
|
||||
/*
|
||||
struct Axis *axis = CtrlGet(ctrl, LEFT, KEY);
|
||||
struct Axis *right = CtrlGet(ctrl, RIGHT, KEY);
|
||||
|
||||
printf("ctrl:%u\n", axis);
|
||||
printf("ctrl:%u\n", right);
|
||||
|
||||
printf("%f\n", axis->last_pressed);
|
||||
|
||||
if (axis->data.axis.is_down) {
|
||||
printf("left");
|
||||
falling.x -= 1;
|
||||
}
|
||||
|
||||
if (right->data.axis.is_down) {
|
||||
printf("right");
|
||||
falling.x += 1;
|
||||
}*/
|
||||
puts(out);
|
||||
|
||||
WindowsWait(0.1);
|
||||
}
|
||||
|
@ -115,8 +89,8 @@ int main()
|
|||
{
|
||||
WindowsInit();
|
||||
|
||||
struct ctrl_bkt bkts[16];
|
||||
Ctrl ctrl = NewCtrl(bkts, 16);
|
||||
struct ctrl_bkt bkts[13];
|
||||
Ctrl ctrl = NewCtrl(bkts, 13);
|
||||
|
||||
for (size_t i = 0; i < 9; i++) {
|
||||
CtrlMap(&ctrl, key_binds[i], key_codes[i], KEY);
|
||||
|
|
422
source/test.cpp
422
source/test.cpp
|
@ -1,422 +0,0 @@
|
|||
#include <iostream>
|
||||
#include <stdlib.h>
|
||||
#include <windows.h>
|
||||
using namespace std;
|
||||
|
||||
void gotoxy(int x, int y);
|
||||
void setcolor(WORD color);
|
||||
void setForeGroundAndBackGroundColor(int ForeGroundColor,int BackGroundColor);
|
||||
void clearscreen();
|
||||
void drawpixel( unsigned char x, unsigned char y, unsigned char Color);
|
||||
void drawpixel2( unsigned char x, unsigned char y, unsigned char Color, char character);
|
||||
void drawcircle(int x, int y, int a, int b, int color);
|
||||
void drawline(int x0, int y0, int x1, int y1, int color);
|
||||
void drawfilledrectangle(unsigned char x1,unsigned char y1,unsigned char x2,unsigned char y2,unsigned char bkcol);
|
||||
void drawframe(unsigned x,unsigned y,unsigned sx,unsigned sy,unsigned char col,unsigned char col2,char text_[]);
|
||||
void drawwindow(unsigned x,unsigned y,unsigned sx,unsigned sy,unsigned char col,unsigned char col2,unsigned char bkcol,char text_[]);
|
||||
|
||||
int main()
|
||||
{
|
||||
gotoxy(1,23);
|
||||
setcolor(7);
|
||||
clearscreen();
|
||||
|
||||
cout<<"click anywhere in console window to write - hello world -\n\n\n\n\n\n\n\n\n\n\n\n\n"
|
||||
"Press Ctrl+C to Exit";
|
||||
|
||||
HANDLE hout= GetStdHandle(STD_OUTPUT_HANDLE);
|
||||
HANDLE hin = GetStdHandle(STD_INPUT_HANDLE);
|
||||
INPUT_RECORD InputRecord;
|
||||
DWORD Events;
|
||||
COORD coord;
|
||||
CONSOLE_CURSOR_INFO cci;
|
||||
cci.dwSize = 25;
|
||||
cci.bVisible = FALSE;
|
||||
SetConsoleCursorInfo(hout, &cci);
|
||||
SetConsoleMode(hin, ENABLE_PROCESSED_INPUT | ENABLE_MOUSE_INPUT);
|
||||
bool EXITGAME = false;
|
||||
int buttonX=1, buttonY=1;
|
||||
|
||||
drawpixel(buttonX,buttonY ,1);
|
||||
gotoxy(buttonX+2,buttonY);
|
||||
setcolor(3);
|
||||
cout<<"<----- a button \n";
|
||||
|
||||
|
||||
while( !EXITGAME )
|
||||
{
|
||||
|
||||
ReadConsoleInput(hin, &InputRecord, 1, &Events);
|
||||
|
||||
|
||||
switch ( InputRecord.EventType ){
|
||||
case KEY_EVENT: // keyboard input
|
||||
|
||||
|
||||
switch (InputRecord.Event.KeyEvent.wVirtualKeyCode)
|
||||
{
|
||||
case VK_ESCAPE:
|
||||
EXITGAME = TRUE;
|
||||
break;
|
||||
|
||||
case VK_SPACE:
|
||||
|
||||
break;
|
||||
|
||||
|
||||
case VK_RETURN:
|
||||
|
||||
break;
|
||||
|
||||
case VK_LEFT:
|
||||
// left key move player left
|
||||
cout<<"VK_LEFT = "<<InputRecord.Event.KeyEvent.wVirtualKeyCode <<" \n";
|
||||
|
||||
break;
|
||||
|
||||
case VK_RIGHT:
|
||||
// right key move player right
|
||||
cout<<"VK_RIGHT = "<<InputRecord.Event.KeyEvent.wVirtualKeyCode <<" \n";
|
||||
|
||||
break;
|
||||
|
||||
case VK_UP:
|
||||
// up key move player up
|
||||
cout<<"VK_UP = "<<InputRecord.Event.KeyEvent.wVirtualKeyCode <<" \n";
|
||||
|
||||
|
||||
break;
|
||||
|
||||
case VK_DOWN:
|
||||
// up key move player down
|
||||
cout<<"VK_DOWN = "<<InputRecord.Event.KeyEvent.wVirtualKeyCode <<" \n";
|
||||
|
||||
|
||||
break;
|
||||
|
||||
|
||||
|
||||
}//switch
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
break;
|
||||
|
||||
case MOUSE_EVENT: // mouse input
|
||||
|
||||
if(InputRecord.Event.MouseEvent.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED)
|
||||
{
|
||||
coord.X = InputRecord.Event.MouseEvent.dwMousePosition.X;
|
||||
coord.Y = InputRecord.Event.MouseEvent.dwMousePosition.Y;
|
||||
SetConsoleCursorPosition(hout,coord);
|
||||
SetConsoleTextAttribute(hout,rand() %7+9);
|
||||
|
||||
if ( (InputRecord.Event.MouseEvent.dwMousePosition.X == buttonX ) &&
|
||||
( InputRecord.Event.MouseEvent.dwMousePosition.Y == buttonY) ){
|
||||
|
||||
clearscreen();
|
||||
gotoxy(1,1);
|
||||
setcolor(7);
|
||||
drawpixel(buttonX,buttonY ,1);
|
||||
setcolor(3);
|
||||
cout<<" mybutton was pressed \n";
|
||||
setcolor(7);
|
||||
Sleep(500);
|
||||
drawpixel(buttonX,buttonY ,1);
|
||||
gotoxy(buttonX+2,buttonY);
|
||||
setcolor(3);
|
||||
cout<<"<----- a button \n";
|
||||
|
||||
|
||||
}
|
||||
|
||||
cout<<"Hello world at "<< InputRecord.Event.MouseEvent.dwMousePosition.X <<" x "<< InputRecord.Event.MouseEvent.dwMousePosition.Y<<" ";
|
||||
|
||||
}// mouse
|
||||
|
||||
break;
|
||||
|
||||
case WINDOW_BUFFER_SIZE_EVENT: // scrn buf. resizing
|
||||
;
|
||||
break;
|
||||
|
||||
case FOCUS_EVENT: // disregard focus events
|
||||
|
||||
case MENU_EVENT: // disregard menu events
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
cout<<"Unknown event type \n";
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
|
||||
FlushConsoleInputBuffer(hin);
|
||||
}
|
||||
gotoxy(1,23);
|
||||
setcolor(7);
|
||||
clearscreen();
|
||||
cout<<"\n";
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
void gotoxy(int x, int y){
|
||||
COORD coord;
|
||||
coord.X = x; coord.Y = y;
|
||||
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
|
||||
return;
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
|
||||
void setcolor(WORD color){
|
||||
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),color);
|
||||
return;
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
|
||||
|
||||
//
|
||||
// colors:
|
||||
// 0 = Black
|
||||
// 1 = Blue
|
||||
// 2 = Green
|
||||
// 3 = Cyan
|
||||
// 4 = Red
|
||||
// 5 = Magenta
|
||||
// 6 = Yellow
|
||||
// 7 = LightGray
|
||||
// 8 = DarkGray
|
||||
// 9 = LightBlue
|
||||
// 10 = LightGreen
|
||||
// 11 = LightCyan
|
||||
// 12 = LightRed
|
||||
// 13 = LightMagenta
|
||||
// 14 = LightYellow
|
||||
// 15 = White
|
||||
|
||||
|
||||
//
|
||||
|
||||
//*****************************************************************************
|
||||
|
||||
void setForeGroundAndBackGroundColor(int ForeGroundColor,int BackGroundColor){
|
||||
int color=16*BackGroundColor+ForeGroundColor;
|
||||
setcolor(color);
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
|
||||
void clearscreen(){
|
||||
COORD coordScreen = { 0, 0 };
|
||||
DWORD cCharsWritten;
|
||||
CONSOLE_SCREEN_BUFFER_INFO csbi;
|
||||
DWORD dwConSize;
|
||||
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||
|
||||
GetConsoleScreenBufferInfo(hConsole, &csbi);
|
||||
dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
|
||||
FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen, &cCharsWritten);
|
||||
GetConsoleScreenBufferInfo(hConsole, &csbi);
|
||||
FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
|
||||
SetConsoleCursorPosition(hConsole, coordScreen);
|
||||
return;
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
|
||||
void drawpixel( unsigned char x, unsigned char y, unsigned char Color){
|
||||
setcolor(Color);
|
||||
gotoxy(x,y);printf("Û");
|
||||
}
|
||||
//*****************************************************************************
|
||||
|
||||
void drawpixel2( unsigned char x, unsigned char y, unsigned char Color, char character){
|
||||
setcolor(Color);
|
||||
gotoxy(x,y);printf("%c",character);
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
|
||||
void drawcircle(int x, int y, int a, int b, int color){
|
||||
int wx, wy;
|
||||
int thresh;
|
||||
int asq = a * a;
|
||||
int bsq = b * b;
|
||||
int xa, ya;
|
||||
|
||||
drawpixel(x, y+b, color);
|
||||
drawpixel(x, y-b, color);
|
||||
|
||||
wx = 0;
|
||||
wy = b;
|
||||
xa = 0;
|
||||
ya = asq * 2 * b;
|
||||
thresh = asq / 4 - asq * b;
|
||||
|
||||
for (;;) {
|
||||
thresh += xa + bsq;
|
||||
|
||||
if (thresh >= 0) {
|
||||
ya -= asq * 2;
|
||||
thresh -= ya;
|
||||
wy--;
|
||||
}
|
||||
|
||||
xa += bsq * 2;
|
||||
wx++;
|
||||
|
||||
if (xa >= ya)
|
||||
break;
|
||||
|
||||
|
||||
drawpixel(x+wx, y-wy, color);
|
||||
drawpixel(x-wx, y-wy, color);
|
||||
drawpixel(x+wx, y+wy, color);
|
||||
drawpixel(x-wx, y+wy, color);
|
||||
}
|
||||
|
||||
drawpixel(x+a, y, color);
|
||||
drawpixel(x-a, y, color);
|
||||
|
||||
wx = a;
|
||||
wy = 0;
|
||||
xa = bsq * 2 * a;
|
||||
|
||||
ya = 0;
|
||||
thresh = bsq / 4 - bsq * a;
|
||||
|
||||
for (;;) {
|
||||
thresh += ya + asq;
|
||||
|
||||
if (thresh >= 0) {
|
||||
xa -= bsq * 2;
|
||||
thresh = thresh - xa;
|
||||
wx--;
|
||||
}
|
||||
|
||||
ya += asq * 2;
|
||||
wy++;
|
||||
|
||||
if (ya > xa)
|
||||
break;
|
||||
|
||||
drawpixel(x+wx, y-wy, color);
|
||||
drawpixel(x-wx, y-wy, color);
|
||||
drawpixel(x+wx, y+wy, color);
|
||||
drawpixel(x-wx, y+wy, color);
|
||||
}
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
|
||||
void drawline(int x0, int y0, int x1, int y1, int color){
|
||||
int pix = color;
|
||||
int dy = y1 - y0;
|
||||
int dx = x1 - x0;
|
||||
int stepx, stepy;
|
||||
|
||||
if (dy < 0) { dy = -dy; stepy = -1; } else { stepy = 1; }
|
||||
if (dx < 0) { dx = -dx; stepx = -1; } else { stepx = 1; }
|
||||
dy <<= 1; // dy is now 2*dy
|
||||
dx <<= 1; // dx is now 2*dx
|
||||
|
||||
drawpixel( x0, y0,pix);
|
||||
if (dx > dy) {
|
||||
int fraction = dy - (dx >> 1); // same as 2*dy - dx
|
||||
while (x0 != x1) {
|
||||
if (fraction >= 0) {
|
||||
y0 += stepy;
|
||||
fraction -= dx; // same as fraction -= 2*dx
|
||||
}
|
||||
x0 += stepx;
|
||||
fraction += dy; // same as fraction -= 2*dy
|
||||
drawpixel( x0, y0,pix);
|
||||
}
|
||||
} else {
|
||||
int fraction = dx - (dy >> 1);
|
||||
while (y0 != y1) {
|
||||
if (fraction >= 0) {
|
||||
x0 += stepx;
|
||||
fraction -= dy;
|
||||
}
|
||||
y0 += stepy;
|
||||
fraction += dx;
|
||||
drawpixel( x0, y0,pix);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
|
||||
|
||||
void drawframe(unsigned x,unsigned y,unsigned sx,unsigned sy,unsigned char col,unsigned char col2,char text_[]){
|
||||
unsigned i,j,m;{
|
||||
|
||||
m=(sx-x); //differential
|
||||
j=m/8; //adjust
|
||||
j=j-1; //more adjustment
|
||||
gotoxy(x,y);printf("É"); //Top left corner of drawframe
|
||||
gotoxy(sx,y);printf("»"); //Top right corner of drawframe
|
||||
gotoxy(x,sy);printf("È"); //Bottom left corner of drawframe
|
||||
gotoxy(sx,sy);printf("¼"); //Bottom right corner of drawframe
|
||||
|
||||
for (i=x+1;i<sx;i++){
|
||||
gotoxy(i,y);printf("Í"); // Top horizontol line
|
||||
gotoxy(i,sy);printf("Í"); // Bottom Horizontal line
|
||||
}
|
||||
|
||||
for (i=y+1;i<sy;i++){
|
||||
gotoxy(x,i);printf("º"); //Left Vertical line
|
||||
gotoxy(sx,i);printf("º"); //Right Vertical Line
|
||||
}
|
||||
|
||||
gotoxy(x+j,y);printf(text_); //put Title
|
||||
gotoxy(1,24);
|
||||
}
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
|
||||
void drawfilledrectangle(unsigned char x1,unsigned char y1,unsigned char x2,unsigned char y2,unsigned char bkcol){
|
||||
int x,y;
|
||||
setcolor(bkcol); //Set to color bkcol
|
||||
|
||||
for (y=y1;y<y2;y++){ //Fill Y Region Loop
|
||||
for (x=x1;x<x2;x++) { //Fill X region Loop
|
||||
gotoxy(x,y);printf(" "); //Draw Solid space
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
|
||||
void drawwindow(unsigned x,unsigned y,unsigned sx,unsigned sy,
|
||||
unsigned char col, unsigned char col2,unsigned char bkcol,char text_[]){
|
||||
drawfilledrectangle(x,y,sx,sy,bkcol);
|
||||
drawframe(x,y,sx,sy,col,col2,text_);
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
|
||||
|
||||
void drawcolorpalette(){
|
||||
for (int i=0;i<16;i++){
|
||||
for (int j=0;j<16;j++){
|
||||
setForeGroundAndBackGroundColor(i,j);
|
||||
gotoxy(i*4,j);printf("%d",(i*j)+1);
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
//*****************************************************************************
|
Loading…
Reference in a new issue