Trouble detecting keyboard inputs while in a game loop

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Caffeine
Member
Member
Posts: 79
Joined: Mon Nov 15, 2021 9:48 pm

Trouble detecting keyboard inputs while in a game loop

Post 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.
Last edited by Caffeine on Fri Feb 25, 2022 2:35 pm, edited 1 time in total.
Octocontrabass
Member
Member
Posts: 5563
Joined: Mon Mar 25, 2013 7:01 pm

Re: Trouble detecting keyboard inputs while in a game loop

Post 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?
Keith
Posts: 2
Joined: Fri Jul 18, 2014 9:34 am

Re: Trouble detecting keyboard inputs while in a game loop

Post 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.
Caffeine
Member
Member
Posts: 79
Joined: Mon Nov 15, 2021 9:48 pm

Re: Trouble detecting keyboard inputs while in a game loop

Post 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.
Last edited by Caffeine on Thu Feb 24, 2022 12:07 pm, edited 1 time in total.
Caffeine
Member
Member
Posts: 79
Joined: Mon Nov 15, 2021 9:48 pm

Re: Trouble detecting keyboard inputs while in a game loop

Post 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.
Caffeine
Member
Member
Posts: 79
Joined: Mon Nov 15, 2021 9:48 pm

Re: Trouble detecting keyboard inputs while in a game loop

Post 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.
Octocontrabass
Member
Member
Posts: 5563
Joined: Mon Mar 25, 2013 7:01 pm

Re: Trouble detecting keyboard inputs while in a game loop

Post 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.)
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Trouble detecting keyboard inputs while in a game loop

Post 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.
Caffeine
Member
Member
Posts: 79
Joined: Mon Nov 15, 2021 9:48 pm

Re: Trouble detecting keyboard inputs while in a game loop

Post 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.
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Trouble detecting keyboard inputs while in a game loop

Post by iansjack »

Caffeine
Member
Member
Posts: 79
Joined: Mon Nov 15, 2021 9:48 pm

Re: Trouble detecting keyboard inputs while in a game loop

Post 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.
Caffeine
Member
Member
Posts: 79
Joined: Mon Nov 15, 2021 9:48 pm

Re: Trouble detecting keyboard inputs while in a game loop

Post 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.
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Trouble detecting keyboard inputs while in a game loop

Post by iansjack »

No. But failure to do points 3 or 4 that I mentioned would mean that you get just one interrupt.
Caffeine
Member
Member
Posts: 79
Joined: Mon Nov 15, 2021 9:48 pm

Re: Trouble detecting keyboard inputs while in a game loop

Post 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?
alexfru
Member
Member
Posts: 1111
Joined: Tue Mar 04, 2014 5:27 am

Re: Trouble detecting keyboard inputs while in a game loop

Post 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.
Post Reply