nnarain / gameboycore-python

Python Bindings for GameboyCore
MIT License
5 stars 2 forks source link

How to use? #8

Open coolq1000 opened 7 years ago

coolq1000 commented 7 years ago

Hi!

I love what you're doing here, But may I ask, how do you use this?

I've tried:

import gameboycore

GBC = gameboycore.GameboyCore()
GBC.open("C:\\Users\\Coolq\\Desktop\\game.gb")

There are no errors, but nothing happens. I cannot find the function to run it. 😕 Could you point me in the right direction? Or even better, show me some code that works?

This is a really cool project, ~Coolq.

coolq1000 commented 7 years ago

UPDATE: This code seems to almost work:

import gameboycore
import pygame,sys

pygame.init()

w,h = 160,144
screen = pygame.display.set_mode((w,h))

GBC = gameboycore.GameboyCore()
GBC.open("C:\\Users\\Coolq\\Desktop\\game.gb")

pixels = []
loc = 0

def getit(pix,a):
    global pixels, loc
    loc = a
    pixels = pix

while True:
    screen.fill((0,0,0))
    if pygame.event.poll().type == pygame.QUIT: pygame.quit(); sys.exit()
    GBC.update(500)
    for i in GBC.getSpriteCache():
        if i.x != 0 and i.y != 0:
            pixel = pixels[loc]
            screen.fill((pixel.r,pixel.g,pixel.b),(i.x,i.y,8,16))
    GBC.registerGpuCallback(getit)
    pygame.display.update()

I'm just guessing all thing, so again actual code from the developer would help a lot ;)

So far, my code only shows tiles, and seems to change it's brightness, there is no background layer, and additionally - no input, sound or detailed tiles ( They look like blocks ).

Thanks, ~Coolq.

nnarain commented 7 years ago

Hello! This project is still under a lot of development. I have some code that works in pygame. Give me a second and I will write up an example of what currently works.

coolq1000 commented 7 years ago

Wow! Fast reply! 😄

Thanks for your time and effort!

Thanks, I've just got to say that I'm a huge fan of emulation and the works of it :)

nnarain commented 7 years ago
import gameboycore
import pygame
import sys

WINDOW_SIZE = (640,480)

class Example(object):
    def __init__(self, dim):
        pygame.init()
        pygame.display.set_caption('GameboyCore Python Example')

        self.dim = dim
        self.screen = pygame.display.set_mode(WINDOW_SIZE)
        # 160x144 is the Gameboy window size
        self.lcd = pygame.Surface((160,144))
        self.done = False

        self.core = gameboycore.GameboyCore()

        self.action_map = {
            pygame.KEYDOWN:gameboycore.KeyAction.ACTION_PRESS,
            pygame.KEYUP:gameboycore.KeyAction.ACTION_RELEASE
        }
        self.key_map = {
            pygame.K_w:gameboycore.JoypadKey.KEY_UP,
            pygame.K_a:gameboycore.JoypadKey.KEY_LEFT,
            pygame.K_d:gameboycore.JoypadKey.KEY_RIGHT,
            pygame.K_s:gameboycore.JoypadKey.KEY_DOWN,
            pygame.K_j:gameboycore.JoypadKey.KEY_A,
            pygame.K_k:gameboycore.JoypadKey.KEY_B,
            pygame.K_RETURN:gameboycore.JoypadKey.KEY_START,
            pygame.K_LSHIFT:gameboycore.JoypadKey.KEY_SELECT
        }

    def run(self, filename):
        self.core.open(filename)
        self.core.registerScanlineCallback(self.scanlineCallback)

        while not self.done:
            self.core.update(512)

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    self.done = True
                elif event.type == pygame.KEYDOWN or event.type == pygame.KEYUP:
                    self.processInput(event.key, event.type)

            pygame.display.flip()

    def processInput(self, key, action):
        gbaction = self.action_map[action]
        gbkey = self.key_map[key]

        self.core.input(gbkey, gbaction)

    def scanlineCallback(self, scanline, line):
        for i in range(len(scanline)):
            pixel = scanline[i]
            self.lcd.set_at((i,line), (pixel.r,pixel.g,pixel.b))
        self.updateScreen()

    def updateScreen(self):
        pygame.transform.scale(self.lcd, self.dim, self.screen)

def main():
    ex = Example(WINDOW_SIZE)
    ex.run(sys.argv[1])

if __name__ == '__main__':
    main()
nnarain commented 7 years ago

Note: I just committed some changes, you will have to reinstall/update gameboycore to version 0.4.0

coolq1000 commented 7 years ago

Ahh! Awesome!

Okay, ATM I have gameboycore v0.3.0. I'll see what I can do to update.

coolq1000 commented 7 years ago

Okay, how can I update? I've install my previous version with pip?

nnarain commented 7 years ago
pip install gameboycore --upgrade
nnarain commented 7 years ago

Also you could try:

pip uninstall gameboycore
pip install gameboycore
coolq1000 commented 7 years ago

facepalm

Thanks :)

That worked, so may I ask what does *.update do? what does the int in the () do? is it the allocated memory?

nnarain commented 7 years ago

GameboyCore::update(int steps) tells the GameboyCore to execute steps CPU steps

The 512 is arbitrary. You could also run update in another thread.

Its calling this function here: https://github.com/nnarain/gameboycore/blob/master/src/core/gameboycore.cpp#L47

coolq1000 commented 7 years ago

what would be the optimal number? Is there any way to speed up the emulator?

nnarain commented 7 years ago

There isn't really an optimal number. The specific reason for this argument in a single threaded application you are drawing to the screen and emulating in the same thread. This can really slow down emulation as drawing is expensive. See example in the gameboy core repo.

No way to speed up. It runs as fast possible

coolq1000 commented 7 years ago

Okay, I found a bunch of things to minimize drawing, but that seems to be the bulk of it, minimizing drawing time, I'll have to go to sleep now, I'll get back to you tomorrow :)

coolq1000 commented 7 years ago

Due to the Documentation tag, I thought this might be a good place to ask ( Correct me if it's not), I would be interesting in writing the README.md for you, if you'd like. If you want a reference of a readme I made for one of my own projects ( A raycaster ), go here.

Would you like me to do this? I'm offering you thing because I feel like you helped me a lot with understanding your project, and I want to share that with everyone else.

Of course you'd be able to review it if you'd like ;)

coolq1000 commented 7 years ago

bump, I know it's only been 22 hours, but I just can't wait for your reply 🤓

nnarain commented 7 years ago

Ya feel free to make a pull request to the readme. Also if you want to can make some additions to the readthedocs pages in the docs folder

coolq1000 commented 7 years ago

Ok, Great! I'll get started!

coolq1000 commented 7 years ago

Ok, so. I've been creating the readme file, but I personally think that mark down is the best one, do you mind if use this? Instead of the .rst file?

coolq1000 commented 7 years ago

Oh, and another idea, is gameboycore able to play audio of the rom?

nnarain commented 7 years ago

I prefer markdown myself. But PyPi doesn't support markdown unfortunately

nnarain commented 7 years ago

GameboyCore currently does not support audio

coolq1000 commented 7 years ago

Ok, thanks for the info, why don't we have two? .rst for PyPi and .md for github?

nnarain commented 7 years ago

I would like to avoid duplication of information and the need to maintain two separate files

coolq1000 commented 7 years ago

what do you want me to do?