Back to blogging ? No and yes , No because I never quit blogging and yes because I wasn't working on any exciting thing that worths sharing. for the past months I spent time on Android development - just some basic stuff - , and reading some C++ books . I found C++ very interesting whatever what people said about how complex it is , it's expressive and efficient that's what matters !!
Back to our talk for today, I was trying to build a Gameboy emulator with C++ . I am new to the experience of emulation so I decided to read a source code to understand the architecture of the device.
There is a lot of open source emulators but I couldn't found a simple documented one , here comes Imran Nazar he tried to write an emulator using javascript, so I'am translating the code into C++.
The implementation of the Z80 - cpu of the Gameboy - requires a set of cpu instructions each implemented as a method of the Cpu class. This set of functions is declared in the .h file of the class and that's more than 600 function prototype !!!!
It's a wast of space and lack of readability to put a prototype per line or all in a single line , so I thought to format the code like a matrix.
I am using Sublime Text 2 because it's awesome ,I am not even going to argue about that, So I think I am lazy enough to write a plugin to do that for me. Python, I started reading a book but I didn't finish it because I found out that's it's a natural language, it doesn't need a book just a cheat sheet and internet connection !!
This article from Nettuts+ is the best reference on the web on building ST2 plugins. The problem that I faced is input , how can I pass an input to my plugin ? I don't have limited number of inputs to use different commands , I think there is a prompt window but even if it exists it's not a good choice because I want to support multiple selections :/ .
My approach is to use the first line of each selection as an input and parse it to specify the format of the matrix, the code is simple :
import sublime, sublime_plugin class MatrixifyCommand(sublime_plugin.TextCommand): def run(self, edit): sels = self.view.sel() for sel in sels: lines = [] separators = [] lengths = [] dim = 0 lines = self.view.substr(sel) lines = lines.splitlines() lines = filter(None, lines) lines = [i.strip() for i in lines] separators = lines[0].split('%') dim = len(separators) - 1 lengths = [0] * dim for i in range(dim): for l in range(i+1,len(lines)-1,dim): lengths[i] = max(lengths[i] , len(lines[l])) output = "" for i in range(1,len(lines)): index = dim-1 if (i%dim)==0 else (i%dim)-1 line = lines[i] output += separators[index] + line + (lengths[(i%dim) -1] - len(line))*" " if (i%dim) == 0: output += separators[dim] + "\n" self.view.replace(edit, sel, output)On my Github repository you can find other files used to specify the command , the Edit menu item and the keyboard shortcut. I sent a pull request to sublime package control , when it's accepted I'll update.
No comments:
Post a Comment