+ getPixel and setPixel with different GraphicsMode and memory usage per pixel
This commit is contained in:
parent
32b66cf8bf
commit
7fa0988413
@ -11,6 +11,7 @@
|
|||||||
|
|
||||||
#include "SharedMemoryAccess.hpp" /* header is important for the shmID. name could be different. maybe not needed cause: (shmget(memory_access_key, NULL, 0)) */
|
#include "SharedMemoryAccess.hpp" /* header is important for the shmID. name could be different. maybe not needed cause: (shmget(memory_access_key, NULL, 0)) */
|
||||||
#include "internal.hpp"
|
#include "internal.hpp"
|
||||||
|
#include "log.hpp"
|
||||||
|
|
||||||
namespace vkvm {
|
namespace vkvm {
|
||||||
constexpr int PERM = 0666; /* access rights */
|
constexpr int PERM = 0666; /* access rights */
|
||||||
|
106
src/vkvm.cpp
106
src/vkvm.cpp
@ -61,29 +61,93 @@ namespace vkvm {
|
|||||||
}
|
}
|
||||||
|
|
||||||
auto setPixel(int x, int y, Color color) -> bool {
|
auto setPixel(int x, int y, Color color) -> bool {
|
||||||
char *ptr = getPixelArea() + (y * getWidth() + x) * 3;
|
lockSharedMemory();
|
||||||
ptr[0] = color.getRed();
|
auto reg = getRegisters();
|
||||||
ptr[1] = color.getGreen();
|
|
||||||
ptr[2] = color.getBlue();
|
switch(reg->graphicMode) {
|
||||||
|
case Text: {
|
||||||
|
int pixelIndex = (y * getWidth() + x);
|
||||||
|
unsigned char *ptr = reinterpret_cast<unsigned char *>(getPixelArea()) + (pixelIndex / 8);
|
||||||
|
if(color == reg->foreground_color){
|
||||||
|
//set bit to 1
|
||||||
|
ptr[0] |= (1 << (pixelIndex % 8));
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
//set bit to 0
|
||||||
|
ptr[0] &= ~(1 << (pixelIndex % 8));
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case TwoColors: {
|
||||||
|
int pixelIndex = (y * getWidth() + x);
|
||||||
|
unsigned char *ptr = reinterpret_cast<unsigned char *>(getPixelArea()) + (pixelIndex / 8);
|
||||||
|
if(color == reg->foreground_color){
|
||||||
|
//set bit to 1
|
||||||
|
ptr[0] |= (1 << (pixelIndex % 8));
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
//set bit to 0
|
||||||
|
ptr[0] &= ~(1 << (pixelIndex % 8));
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case Gray_256: {
|
||||||
|
unsigned char *ptr = reinterpret_cast<unsigned char *>(getPixelArea()) + (y * getWidth() + x) * 1;
|
||||||
|
int avg = color.getRed() + color.getGreen() + color.getBlue();
|
||||||
|
avg /= 3;
|
||||||
|
ptr[0] = (unsigned char)avg;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case RGB: {
|
||||||
|
unsigned char *ptr = reinterpret_cast<unsigned char *>(getPixelArea()) + (y * getWidth() + x) * 3;
|
||||||
|
ptr[0] = (unsigned char)color.getRed();
|
||||||
|
ptr[1] = (unsigned char)color.getGreen();
|
||||||
|
ptr[2] = (unsigned char)color.getBlue();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
unlockSharedMemory();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto getPixel(int x, int y) -> Color {
|
auto getPixel(int x, int y) -> Color {
|
||||||
|
Color color = Color(0,0,0);
|
||||||
|
auto reg = getRegisters();
|
||||||
|
|
||||||
|
switch(reg->graphicMode) {
|
||||||
|
case Text: {
|
||||||
|
int pixelIndex = (y * getWidth() + x);
|
||||||
|
unsigned char *ptr = reinterpret_cast<unsigned char *>(getPixelArea()) + pixelIndex / 8;
|
||||||
|
bool bit = (bool)(ptr[0] & (1 << (pixelIndex % 8)));
|
||||||
|
if(bit){
|
||||||
|
color = reg->foreground_color;
|
||||||
|
}else{
|
||||||
|
color =reg->background_color;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case TwoColors: {
|
||||||
|
int pixelIndex = (y * getWidth() + x);
|
||||||
|
unsigned char *ptr = reinterpret_cast<unsigned char *>(getPixelArea()) + (pixelIndex / 8);
|
||||||
|
bool bit = (bool)(ptr[0] & (1 << (pixelIndex % 8)));
|
||||||
|
if(bit){
|
||||||
|
color = reg->foreground_color;
|
||||||
|
}else{
|
||||||
|
color =reg->background_color;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case Gray_256: {
|
||||||
|
unsigned char *ptr = reinterpret_cast<unsigned char *>(getPixelArea()) + (y * getWidth() + x) * 1;
|
||||||
|
color = {ptr[0], ptr[0], ptr[0]};
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case RGB: {
|
||||||
unsigned char *ptr = reinterpret_cast<unsigned char *>(getPixelArea()) + (y * getWidth() + x) * 3;
|
unsigned char *ptr = reinterpret_cast<unsigned char *>(getPixelArea()) + (y * getWidth() + x) * 3;
|
||||||
|
color = {ptr[0], ptr[1], ptr[2]};
|
||||||
Color color = {ptr[0], ptr[1], ptr[2]};
|
break;
|
||||||
|
|
||||||
if(getMode() == GraphicMode::TwoColors || getMode() == GraphicMode::Text) {
|
|
||||||
if(color != getForegroundColor()) {
|
|
||||||
color = getBackgroundColor();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
else if(getMode() == GraphicMode::Gray_256) {
|
|
||||||
unsigned char average = (color.getBlue() + color.getGreen() + color.getRed()) / 3;
|
|
||||||
color = {average, average, average};
|
|
||||||
}
|
|
||||||
|
|
||||||
return color;
|
return color;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -211,14 +275,14 @@ namespace vkvm {
|
|||||||
|
|
||||||
auto getLastPressedKey() -> KeyCode {
|
auto getLastPressedKey() -> KeyCode {
|
||||||
lockSharedMemory();
|
lockSharedMemory();
|
||||||
|
KeyCode keyCode = KeyCode(0);
|
||||||
auto reg = getRegisters();
|
auto reg = getRegisters();
|
||||||
if(reg->keyboardBuffer_index_read == reg->keyboardBuffer_index_write){
|
if(reg->keyboardBuffer_index_read != reg->keyboardBuffer_index_write) {
|
||||||
return KeyCode(0);
|
keyCode = (KeyCode) reg->keyboardBuffer[reg->keyboardBuffer_index_read++];
|
||||||
}
|
if (reg->keyboardBuffer_index_read >= sizeof(reg->keyboardBuffer)) {
|
||||||
KeyCode keyCode = (KeyCode)reg->keyboardBuffer[reg->keyboardBuffer_index_read++];
|
|
||||||
if(reg->keyboardBuffer_index_read >= sizeof(reg->keyboardBuffer)){
|
|
||||||
reg->keyboardBuffer_index_read = 0;
|
reg->keyboardBuffer_index_read = 0;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
unlockSharedMemory();
|
unlockSharedMemory();
|
||||||
return keyCode;
|
return keyCode;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user