• Advertisement
Sign in to follow this  
  • entries
    2
  • comments
    0
  • views
    174

About this blog

C-Lesh is a programming language for video games that I'm doing. It is everything I want in a language. Current progress is here: https://github.com/codeloadernet/c-lesh

You can also learn about the language as well: http://www.codeloader.net/readme.html

Entries in this blog

Currently, I'm coding the memory mapper for the interpreter - the routine that let's the interpreter talk to SFML in order to do I/O. It is kind of like a driver or something like that. Here I show how to read/write values to the mapper.

// Basic Memory Map
//
// your program
// sprite register
// sound register
// music register
// text register
// signal register
// camera register
// collision register
// level register
// progress register
void Cclsh::Rmemory_mapper(int address, std::string field, Fvalue data) {
int prgm_low = 0;
int prgm_high = prgm_low + this->memory_size;
int sprite_addr = prgm_high;
int sound_addr = sprite_addr + 1;
int music_addr = sound_addr + 1;
int text_addr = music_addr + 1;
int signal_addr = text_addr + 1;
int camera_addr = signal_addr + 1;
int collision_addr = camera_addr + 1;
int level_addr = collision_addr + 1;
int progress_addr = level_addr + 1;
if ((address >= prgm_low) && (address < prgm_high)) {
Fblock* block = &this->memory[address];
if (data.type == 0) { // Read
if (field.length() == 0) { // Read value.
data.string = block->value.string;
data.number = block->value.number;
data.type = block->value.type;
}
else { // Read field.
if (block->fields.find(field) != block->fields.end()) {
data.string = block->fields[field].string;
data.number = block->fields[field].number;
data.type = block->fields[field].type;
}
else { // Field not defined.
data.string = "null";
data.number = 0;
data.type = 2; // String
}
}
}
else { // Write
if (field.length == 0) { // Write value.
block->value.string = data.string;
block->value.number = data.number;
block->value.type = data.type;
}
else { // Write field.
if (block->fields.find(field) != block->fields.end()) {
block->fields[field].string = data.string;
block->fields[field].number = data.number;
block->fields[field].type = data.type;
}
else {
Fvalue new_data;
new_data.string = data.string;
new_data.number = data.number;
new_data.type = data.type;
block.fields[field] = new_data;
}
}
}
}
}

You can take a look at this here but basically I'm converting keyboard, mouse, and gamepad input into signals and then buffering them and removing one signal per frame. I think this is ingenious!

void Cio::Rprocess() {
sf::Clock clock;
// Check for gamepad connection.
if (sf::Joystick::isConnected(0)) {
if (sf::Joystick::getButtonCount(0) == BUTTON_COUNT) {
this->gamepad_connected = true;
}
}
// Try to load gamepad map.
std::ifstream config_file("gamepad.cfg");
if (config_file) {
for (int button_index = 0; button_index < BUTTON_COUNT; button_index++) {
config_file >> this->button_map[button_index];
}
}
else {
this->Rmap_gamepad(); // Do a mapping.
}
// Do window processing here and block.
bool paused = false;
while (!this->window->isOpen()) {
sf::Event event;
while (window->pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window->close();
}
if (event.type == sf::Event::LostFocus) {
paused = true;
}
if (event.type == sf::Event::GainedFocus) {
paused = true;
}
if (!paused) {
Fsignal signal;
signal.code = 0;
signal.x = 0;
signal.y = 0;
signal.key = 0;
signal.button = 0;
// Process mouse and keyboard events.
this->Rprocess_keys();
this->Rprocess_mouse();
// Process mouse movement.
sf::Vector2i coords = sf::Mouse::getPosition();
if (event.type == sf::Event::MouseMoved) {
signal.code = SIGNAL_MOVE;
signal.x = coords.x;
signal.y = coords.y;
this->sig_buffer.push_back(signal);
}
else if (event.type == sf::Event::MouseEntered) {
signal.code = SIGNAL_ENTERED;
signal.x = coords.x;
signal.y = coords.y;
this->sig_buffer.push_back(signal);
}
else if (event.type == sf::Event::MouseLeft) {
signal.code = SIGNAL_LEAVE;
signal.x = coords.x;
signal.y = coords.y;
this->sig_buffer.push_back(signal);
}
// Process the gamepad.
if (event.type == sf::Event::JoystickConnected) {
this->gamepad_connected = true;
}
if (event.type == sf::Event::JoystickDisconnected) {
this->gamepad_connected = false;
}
this->Rprocess_gamepad();
// Generate a blanking signal.
sf::Time elapsed = clock.getElapsedTime();
if (elapsed.asMilliseconds() >= (1000 / FPS)) {
clock.restart();
// Grab a signal and store it in the register.
if (this->sig_buffer.size() > 0) {
this->signal_reg = this->sig_buffer.front();
this->sig_buffer.erase(this->sig_buffer.begin());
}
// Clear back buffer.
this->back_buffer->clear();
// Run code here.
}
}
}
// Do buffer rendering.
this->back_buffer->display();
sf::Texture& texture = this->back_buffer->getTexture();
sf::Sprite buffer_sprite(texture);
window->draw(buffer_sprite);
// Do window rendering.
window->clear();
window->display();
}
}
 
void Cio::Rprocess_keys() {
Fsignal signal;
signal.code = 0;
signal.x = 0;
signal.y = 0;
signal.key = 0;
signal.button = 0;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
signal.code = SIGNAL_LEFT;
this->sig_buffer.push_back(signal);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
signal.code = SIGNAL_RIGHT;
this->sig_buffer.push_back(signal);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
signal.code = SIGNAL_UP;
this->sig_buffer.push_back(signal);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) {
signal.code = SIGNAL_DOWN;
this->sig_buffer.push_back(signal);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Z)) {
signal.code = SIGNAL_ACTION;
signal.button = BUTTON_Y;
this->sig_buffer.push_back(signal);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::X)) {
signal.code = SIGNAL_ACTION;
signal.button = BUTTON_B;
this->sig_buffer.push_back(signal);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::C)) {
signal.code = SIGNAL_ACTION;
signal.button = BUTTON_A;
this->sig_buffer.push_back(signal);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
signal.code = SIGNAL_ACTION;
signal.button = BUTTON_X;
this->sig_buffer.push_back(signal);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Return)) {
signal.code = SIGNAL_ACTION;
signal.button = BUTTON_START;
this->sig_buffer.push_back(signal);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::RShift)) {
signal.code = SIGNAL_ACTION;
signal.button = BUTTON_SELECT;
this->sig_buffer.push_back(signal);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::C)) {
signal.code = SIGNAL_ACTION;
signal.button = BUTTON_A;
this->sig_buffer.push_back(signal);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Num1)) {
signal.code = SIGNAL_ACTION;
signal.button = BUTTON_L;
this->sig_buffer.push_back(signal);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Num2)) {
signal.code = SIGNAL_ACTION;
signal.button = BUTTON_R;
this->sig_buffer.push_back(signal);
}
}
 
void Cio::Rprocess_mouse() {
Fsignal signal;
signal.code = 0;
signal.x = 0;
signal.y = 0;
signal.key = 0;
signal.button = 0;
if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
signal.code = SIGNAL_MOUSE;
sf::Vector2i position = sf::Mouse::getPosition();
signal.x = position.x;
signal.y = position.y;
signal.button = BUTTON_LEFT;
this->sig_buffer.push_back(signal);
}
else if (sf::Mouse::isButtonPressed(sf::Mouse::Right)) {
signal.code = SIGNAL_MOUSE;
sf::Vector2i position = sf::Mouse::getPosition();
signal.x = position.x;
signal.y = position.y;
signal.button = BUTTON_RIGHT;
this->sig_buffer.push_back(signal);
}
}
 
void Cio::Rprocess_gamepad() {
if (this->gamepad_connected) {
Fsignal signal;
signal.code = 0;
signal.x = 0;
signal.y = 0;
signal.key = 0;
signal.button = 0;
// Poll the gamepad to update positions.
sf::Joystick::update();
float x_pos = sf::Joystick::getAxisPosition(0, sf::X);
float y_pos = sf::Joystick::getAxisPosition(0, sf::Y);
if (x_pos == -100.0) { // Values should be standardized.
signal.code = SIGNAL_LEFT;
this->sig_buffer.push_back(signal);
}
else if (x_pos == 100.0) {
signal.code = SIGNAL_RIGHT;
this->sig_buffer.push_back(signal);
}
if (y_pos == -100.0) {
signal.code = SIGNAL_UP;
this->sig_buffer.push_back(signal);
}
else if (y_pos == 100.0) {
signal.code = SIGNAL_DOWN;
this->sig_buffer.push_back(signal);
}
// Check for button presses using button map.
for (int button_index = 0; button_index < BUTTON_COUNT; button_index++) {
int button = this->button_map[button_index];
if (sf::Joystick::isButtonPressed(0, button)) {
signal.button = this->button_signal_map[button_index]; // Assign the button signal.
this->sig_buffer.push_back(signal);
break;
}
}
}
}
Sign in to follow this  
  • Advertisement