+ getPixel and setPixel with different GraphicsMode and memory usage per pixel

This commit is contained in:
Julian Hinxlage 2019-11-26 12:33:34 +01:00
parent 32b66cf8bf
commit 7fa0988413
2 changed files with 87 additions and 22 deletions

View File

@ -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 "internal.hpp"
#include "log.hpp"
namespace vkvm {
constexpr int PERM = 0666; /* access rights */

View File

@ -61,29 +61,93 @@ namespace vkvm {
}
auto setPixel(int x, int y, Color color) -> bool {
char *ptr = getPixelArea() + (y * getWidth() + x) * 3;
ptr[0] = color.getRed();
ptr[1] = color.getGreen();
ptr[2] = color.getBlue();
lockSharedMemory();
auto reg = getRegisters();
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;
}
auto getPixel(int x, int y) -> Color {
unsigned char *ptr = reinterpret_cast<unsigned char *>(getPixelArea()) + (y * getWidth() + x) * 3;
Color color = Color(0,0,0);
auto reg = getRegisters();
Color color = {ptr[0], ptr[1], ptr[2]};
if(getMode() == GraphicMode::TwoColors || getMode() == GraphicMode::Text) {
if(color != getForegroundColor()) {
color = getBackgroundColor();
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;
color = {ptr[0], ptr[1], ptr[2]};
break;
}
}
else if(getMode() == GraphicMode::Gray_256) {
unsigned char average = (color.getBlue() + color.getGreen() + color.getRed()) / 3;
color = {average, average, average};
}
return color;
}
@ -211,13 +275,13 @@ namespace vkvm {
auto getLastPressedKey() -> KeyCode {
lockSharedMemory();
KeyCode keyCode = KeyCode(0);
auto reg = getRegisters();
if(reg->keyboardBuffer_index_read == reg->keyboardBuffer_index_write){
return KeyCode(0);
}
KeyCode keyCode = (KeyCode)reg->keyboardBuffer[reg->keyboardBuffer_index_read++];
if(reg->keyboardBuffer_index_read >= sizeof(reg->keyboardBuffer)){
reg->keyboardBuffer_index_read = 0;
if(reg->keyboardBuffer_index_read != reg->keyboardBuffer_index_write) {
keyCode = (KeyCode) reg->keyboardBuffer[reg->keyboardBuffer_index_read++];
if (reg->keyboardBuffer_index_read >= sizeof(reg->keyboardBuffer)) {
reg->keyboardBuffer_index_read = 0;
}
}
unlockSharedMemory();
return keyCode;