Jumping Ball

Jumping Ball

Progress Report 1

Working on the windows version

Just as expected. More weird character string memory problems. It is unfortunate that I have to rewrite the code that manages character input and deletion. After some experimentation, I managed to fix the character input. I deleted most memory allocations and used character arrays instead. The arrays are very easy to use compared to char*.

To fix the bug, I used temporary variables and loops to edit the input content while the user inputs new values. Also, I made sure that the terminating character is properly added to the end of the string. The program does not need 1024 characters. I need to fix this on the next update.

main.c

char *newCharacter = events.text.text;
char cpyTemp[1024];

if (strlen(interiorTextBox.content) == 0)
{
     for (int i = 0; i < strlen(newCharacter); i++)
     {
        interiorTextBox.content[i] = newCharacter[i];
     }
}
else
{
    for (int i = 0; i < strlen(interiorTextBox.content); i++)
    {
        cpyTemp[i] = interiorTextBox.content[i];
    }

    cpyTemp[strlen(interiorTextBox.content)] = newCharacter[0];
    cpyTemp[strlen(interiorTextBox.content) + 1] = '\0';

    for (int k = 0; k < strlen(cpyTemp); k++)
    {
        interiorTextBox.content[k] = cpyTemp[k];
    }

    interiorTextBox.content[strlen(cpyTemp)] = '\0';
}        

There was a need to edit the textbox.c source file too. The cursor of the text box appears at the end of the input. The temp variable is used to display the user’s input on the screen.

textbox.c

char temp[5];

if (strlen(interiorTextBox.content) > 0)
{
    for (int i = 0; i < strlen(interiorTextBox.content); i++)
    {
        temp[i] = interiorTextBox.content[i];
    }

    temp[strlen(interiorTextBox.content)] = 'I';
    temp[strlen(interiorTextBox.content) + 1] = '\0';
}
else
{
    temp[0] = 'I';
    temp[1] = '\0';
}        

I still need to fix the logic for the character deletion. Avoiding memory allocation is a good way to increase performance and headaches compared to using char*, calloc, and malloc. I should move the new code to the main working branch when it’s finished.

Original Post

https://therunner.digital/jumping-ball/devlog/2024/07/23/jumpingball-progress-1.html

要查看或添加评论,请登录

Chris Douris的更多文章

  • Jumping Ball Progress Report 4

    Jumping Ball Progress Report 4

    Completing the Windows version Creating an installer for my program was elementary. I only needed to install an…

  • MotionMedia - An Introduction

    MotionMedia - An Introduction

    What is MotionMedia? MotionMedia is a command line application made with C++ for the Raspberry Pi. The program will use…

  • Jumping Ball - Progress Report 3

    Jumping Ball - Progress Report 3

    Working on MacOS and Windows Versions I updated the MacOS source to code use char arrays instead of char* to fix memory…

  • Jumping Ball - Progress Report 2

    Jumping Ball - Progress Report 2

    Progress with the windows version I worked on the completing the input logic for the Jumping Ball application. Figuring…

  • Rebuild Back Better - Progress Report 1

    Rebuild Back Better - Progress Report 1

    Upgrade Buttons Upgrades are a very important feature for any clicker game that exists. They give a sense of…

  • Rebuild Back Better

    Rebuild Back Better

    Progress Report 1 I am happy to start working on RBB again. This time, I added the background image to the Main Menu…

  • Jumping Ball Progress

    Jumping Ball Progress

    Blog Post 3 I need to post more updates on my progress. I learned how to use CMake's install features to create an…

  • Jumping Ball Progress

    Jumping Ball Progress

    Blog Post 2 I finally figured out how to fix the font path crash. Using wasn't enough, as many crashes suddenly…

  • Jumping Ball Progress

    Jumping Ball Progress

    Blog Post 1 It took me quite a bit to figure out how to handle the ball's behaviour. Using print statements and…

  • Day 100/100 100 Days of Code

    Day 100/100 100 Days of Code

    This is the last day of 100 days of code. For the last day I watched tutorials and read about multithreading with C…

社区洞察

其他会员也浏览了