Page 1 of 2

Trouble detecting keyboard inputs while in a game loop

Posted: Wed Feb 23, 2022 2:04 pm
by Caffeine
Hi, I was wondering if there is a method to create game loops in my OS. I am trying to make pong, and just got double buffering working. I tried to stick the code in a loop, but my keyboard interrupt is not being called when in the loop.

Here is my code:
#include "../Kernel/system.h"
#include "../Drivers/vga.h"
#include "../Graphics/double_buffer.h"

int gameRunning = 0;
int it = 0;

Code: Select all

void mainLoop() {


    wait(16);

    clearBuffer();

    unsigned int c = 0xffffff;
    // plotPixle(3, 30, c);
    // plotPixle(4, 30, c);
    // plotPixle(5, 30, c);
    // plotPixle(6, 30, c);
    // plotPixle(7, 30, c);

    // drawRectangleBuffer(10+it, 30, 30, 40, c);

    swapBuffers();
    it = it + 1;
    mainLoop();
    //gameRunning = 0;

    
}

// Handle a keypress
void pongHandler(char c) {
    drawRectangleBuffer(10, 30, 30, 40, c);
    swapBuffers();
}

void loadPong() {
    changeProcess("pong");
    gameRunning = 1;

    mainLoop();
}
Here is an explanation as to what this does. And this code is just a placeholder, I'll change it when I get this to work.

wait(ms) halts the cpu for however many ms specified
drawRectangleBuffer() Draws a rectangle to the backbuffer
swapBuffers() Swaps the backbuffer with video memory
changeProcess() Changes where a keyboard input will go. In this case, it goes to pongHandler().

It works when it's not in a loop. Any solutions to this problem?

EDIT: No interrupts are being called while in the loop.

Re: Trouble detecting keyboard inputs while in a game loop

Posted: Wed Feb 23, 2022 7:22 pm
by Octocontrabass
Caffeine wrote:my keyboard interrupt is not being called when in the loop.
How do you know this is what's happening? Did you set a breakpoint with your debugger?

Re: Trouble detecting keyboard inputs while in a game loop

Posted: Thu Feb 24, 2022 7:02 am
by Keith
While this may not be related to the problem at hand, your mainLoop function is recursive, so you will have problems with stack overflow.

Re: Trouble detecting keyboard inputs while in a game loop

Posted: Thu Feb 24, 2022 12:05 pm
by Caffeine
Octocontrabass wrote:
Caffeine wrote:my keyboard interrupt is not being called when in the loop.
How do you know this is what's happening? Did you set a breakpoint with your debugger?
No, I'll figure out how to do this. But I have changed out the function to make sure that that was the issue. So I know it has something to do with my keyboard interrupts.

Re: Trouble detecting keyboard inputs while in a game loop

Posted: Thu Feb 24, 2022 12:05 pm
by Caffeine
Keith wrote:While this may not be related to the problem at hand, your mainLoop function is recursive, so you will have problems with stack overflow.
Thanks, I'll change that.

Re: Trouble detecting keyboard inputs while in a game loop

Posted: Thu Feb 24, 2022 2:47 pm
by Caffeine
Octocontrabass wrote:
Caffeine wrote:my keyboard interrupt is not being called when in the loop.
How do you know this is what's happening? Did you set a breakpoint with your debugger?
To confirm - the Keyboard interrupt is not being called. I set up a breakpoint and it is not going off while in the loop.

Re: Trouble detecting keyboard inputs while in a game loop

Posted: Thu Feb 24, 2022 7:23 pm
by Octocontrabass
Caffeine wrote:To confirm - the Keyboard interrupt is not being called. I set up a breakpoint and it is not going off while in the loop.
Have you set EFLAGS.IF?

How and when do you initialize the interrupt controllers? (It should be done before you initialize the keyboard controller.)

How and when do you initialize the keyboard controller? (Usually you don't need to initialize much - just empty the output buffer.)

Re: Trouble detecting keyboard inputs while in a game loop

Posted: Fri Feb 25, 2022 3:03 am
by iansjack
There are really only four things that can stop keyboard interrupts.

1. The PIC must be programmed to pass the interrupt to the processor.

2 The processors IF flag must be enabled.

3. The keyboard port must be read after each interrupt to clear its internal buffer.

4. The PIC must be acknowledged that the interrupt has occurred.

5. Other faults (invalid interrupt handler, invalid stack, etc.) would lead to a processor exception and/or a reboot.

If you are absolutely sure that no keyboard interrupts are being fired then you have not done one of the above.

Re: Trouble detecting keyboard inputs while in a game loop

Posted: Fri Feb 25, 2022 8:32 am
by Caffeine
iansjack wrote:There are really only four things that can stop keyboard interrupts.

1. The PIC must be programmed to pass the interrupt to the processor.

2 The processors IF flag must be enabled.

3. The keyboard port must be read after each interrupt to clear its internal buffer.

4. The PIC must be acknowledged that the interrupt has occurred.

5. Other faults (invalid interrupt handler, invalid stack, etc.) would lead to a processor exception and/or a reboot.

If you are absolutely sure that no keyboard interrupts are being fired then you have not done one of the above.
Any recourses on how to do these? Here is the OS code: https://github.com/zachary-d-r/CaffeineOS

The interrupt code is in CPU.

Re: Trouble detecting keyboard inputs while in a game loop

Posted: Fri Feb 25, 2022 9:17 am
by iansjack

Re: Trouble detecting keyboard inputs while in a game loop

Posted: Fri Feb 25, 2022 9:18 am
by Caffeine
iansjack wrote:You could start here: https://wiki.osdev.org/I_Can%27t_Get_Interrupts_Working
Thanks! I'll take a look at this.

Re: Trouble detecting keyboard inputs while in a game loop

Posted: Fri Feb 25, 2022 2:24 pm
by Caffeine
Is there something about loops that makes it so interrupts are not being called. I have just realized that no interrupts are being called while in the loop.

Re: Trouble detecting keyboard inputs while in a game loop

Posted: Fri Feb 25, 2022 3:27 pm
by iansjack
No. But failure to do points 3 or 4 that I mentioned would mean that you get just one interrupt.

Re: Trouble detecting keyboard inputs while in a game loop

Posted: Fri Feb 25, 2022 4:39 pm
by Caffeine
iansjack wrote:No. But failure to do points 3 or 4 that I mentioned would mean that you get just one interrupt.
What do you mean by just get one interrupt?

Re: Trouble detecting keyboard inputs while in a game loop

Posted: Fri Feb 25, 2022 8:44 pm
by alexfru
Caffeine wrote:
iansjack wrote:No. But failure to do points 3 or 4 that I mentioned would mean that you get just one interrupt.
What do you mean by just get one interrupt?
The interrupt controller and the keyboard need to know that the CPU has finished handling the interrupt before there's another one generated and delivered to the CPU.