Added file loading support

This commit is contained in:
Morten Delenk 2015-12-14 19:10:30 +01:00
parent 7cfb7eb735
commit a99fad5338
10 changed files with 156 additions and 7 deletions

4
.kateproject Normal file
View file

@ -0,0 +1,4 @@
{
"name": "kssed",
"files": [ { "git": 1 } ]
}

0
.kateproject.d/notes.txt Normal file
View file

View file

@ -1,7 +1,7 @@
include nall/GNUmakefile
target = kssed
# compiler
flags += -I. -g3 -Werror #-O3
flags += -I. -g -fno-wrapv -Werror #-O3
objects := libco
# profile-guided optimization mode
@ -32,7 +32,7 @@ ifeq ($(platform),windows)
else ifeq ($(platform),macosx)
flags += -march=native
else ifeq ($(platform),linux)
flags += -march=native -fopenmp
#flags += -march=native -fopenmp
link += -fopenmp
link += -Wl,-export-dynamic
link += -lX11 -lXext -ldl

View file

@ -1,5 +1,5 @@
name := kssed
ui_objects := ui-kssed ui-config ui-mainwin
ui_objects := ui-kssed ui-config ui-mainwin ui-rom
ui_objects += ruby hiro
ui_objects += $(if $(call streq,$(platform),windows),ui-resource)
ifeq ($(platform),windows)
@ -32,6 +32,7 @@ obj/hiro.o: hiro/hiro.cpp $(call rwildcard,hiro/)
obj/ui-kssed.o: $(ui)/kssed.cpp $(call rwildcard,$(ui)/)
obj/ui-config.o: $(ui)/configuration/config.cpp $(call rwildcard,$(ui)/)
obj/ui-mainwin.o: $(ui)/mainwin/mainwindow.cpp $(call rwildcard,$(ui)/)
obj/ui-rom.o: $(ui)/rom/rom.cpp $(call rwildcard,$(ui)/)
obj/ui-resource.o:
windres data/resource.rc obj/ui-resource.o
build: $(objects)
@ -46,11 +47,7 @@ else
mkdir -p $(prefix)/bin/
mkdir -p $(prefix)/share/icons/
mkdir -p $(prefix)/$(name)/
mkdir -p ~/Emulation/System/
cp out/$(name) $(prefix)/bin/$(name)
cp data/higan.png $(prefix)/share/icons/$(name).png
cp data/cheats.bml $(prefix)/$(name)/cheats.bml
cp -R profile/* ~/Emulation/System/
endif
uninstall:

View file

@ -6,6 +6,7 @@ using namespace ruby;
using namespace hiro;
#include "configuration/config.hpp"
#include "mainwin/mainwindow.hpp"
#include "rom/rom.hpp"
extern Video* video;
extern Audio* audio;
extern Input* input;

View file

@ -1,8 +1,56 @@
#include "../kssed.hpp"
#include <stdint.h>
#include <iostream>
mainWindow* mainwin=nullptr;
mainWindow::mainWindow() {
mainwin=this;
fileMenu.setText("File");
loadFile.setText("Open...").onActivate([=] {
auto location = BrowserDialog()
.setTitle("Load Kirby Super Star/Kirby's Fun Pak/Kirby Super Deluxe ROM")
.setPath("/")
.setFilters("Unheadered SNES ROM|*.sfc")
.openFile();
try {
new ROM(location,[](int addr)->int{
if(addr > 0xFFFFFF)
return -1;
if((addr < 0xC00000)&&!(addr&0x8000))
return -1;
if((addr < 0x800000)&&(addr >= 0x400000))
return -1;
unsigned char bank=(unsigned char)(addr>>16);
if((bank<0xC0)&&(bank>0x7F))
bank-=0x40;
std::cout<<std::hex<<addr<<"->"<<std::hex<<((addr&0x7FFF)+(bank<<15))<<" or "<<std::hex<<(addr-0xC0000)<<std::endl;
if(bank<0x80)
return (addr&0x7FFF)+(bank<<15);
else
return addr-0xC00000;
});
//Checking if ROM is a valid Kirby ROM
char name[]="KIRBY SUPER DELUXE ";
//The name is same for all 4 revisions of the game (NTSC J, NTSC J 2, NTSC U, PAL)
for(int i=0xFFC0;i<0xFFD4;i++) {
if((char)((*rom)[i])!=name[i-0xFFC0])
throw (string{"This is not a valid ROM for this program! (Wanted ",name[i-0xFFC0]," got ", (char)((*rom)[i]),"!"}).data();
}
mainwin->loadFile.setEnabled(false);
mainwin->saveFile.setEnabled(true);
mainwin->closeFile.setEnabled(true);
} catch(const char* msg){if(rom) { delete rom; rom=nullptr;}
std::cerr<<msg<<std::endl;}
});
saveFile.setText("Save").onActivate([] {rom->save();});
closeFile.setText("Close").onActivate([] {
delete rom;
rom=nullptr;
mainwin->loadFile.setEnabled(true);
mainwin->saveFile.setEnabled(false);
mainwin->closeFile.setEnabled(false);
});
mainwin->saveFile.setEnabled(false);
mainwin->closeFile.setEnabled(false);
setGeometry({32,32,640,480});
setResizable(false);
setBackgroundColor({0,0,0});

View file

@ -1,6 +1,11 @@
struct mainWindow : Window {
mainWindow();
MenuBar menuBar{this};
Menu fileMenu{&menuBar};
MenuItem loadFile{&fileMenu};
MenuItem saveFile{&fileMenu};
MenuItem closeFile{&fileMenu};
FixedLayout layout{this};
Viewport viewport{&layout, Geometry{0, 0, 1, 1}};
};

Binary file not shown.

57
target-kssed/rom/rom.cpp Normal file
View file

@ -0,0 +1,57 @@
#include "../kssed.hpp"
#include <iostream>
#include <fstream>
ROM* rom=nullptr;
ROM::ROM(string filename, int(*addrResolv)(int)): filename(filename),addrResolv(addrResolv) {
std::ifstream file(filename.data(),std::ios::in|std::ios::binary);
::rom=this;
if(file.is_open()) {
file.read((char *)rom,sizeof(rom));
file.close();
} else {
throw "Cannot read file!"; //TODO
}
}
ROM::~ROM() {
}
auto ROM::save() -> void {
std::ofstream file(filename.data(),std::ios::out|std::ios::binary);
if(file.is_open()) {
file.write((char *)rom,sizeof(rom));
file.close();
} else {
throw "Cannot write file!"; //TODO
}
}
auto ROM::operator[](int addr)->unsigned char & {
int offset=addrResolv(addr);
if(offset==-1)
return freeMemory;
else
return rom[offset];
}
RGBA8888::RGBA8888(char red, char green, char blue, char alpha): red(red), green(green), blue(blue), alpha(alpha) {}
RGBA8888::RGBA8888(int color) {
alpha=(char)(color>>24);
red=(char)(color>>16);
green=(char)(color>>8);
blue=(char)color;
}
RGBA8888::operator int() {
return (alpha<<24)+(red<<16)+(green<<8)+blue;
}
RGBA8888::operator struct RGB555() {
return RGB555((char)(red>>3),(char)(green>>3),(char)(blue>>3));
}
RGB555::RGB555(char red, char green, char blue): red(red), green(green), blue(blue) {}
RGB555::RGB555(short color) {
red=(char)(color & 0x1F);
green=(char)((color>>5)&0x1F);
blue=(char)((color>>10)&0x1F);
}
RGB555::operator short() {
return red+(green<<5)+(blue<<10);
}
RGB555::operator struct RGBA8888() {
return RGBA8888(red<<3,green<<3,blue<<3,-1);
}

View file

@ -0,0 +1,37 @@
#include <functional>
class ROM {
private:
string filename;
unsigned char rom[4*1024*1024];
unsigned char freeMemory;
int (*addrResolv)(int);
ROM(){}
public:
ROM(string filename, int(*addrResolv)(int));
auto save() -> void;
auto setAddrResolver(int (*addrResolv)(int)) -> void {
this->addrResolv=addrResolv;
}
auto operator[](int addr)->unsigned char &;
~ROM();
};
extern ROM* rom;
struct RGBA8888 {
char red;
char green;
char blue;
char alpha;
RGBA8888(char red, char green, char blue, char alpha);
RGBA8888(int color);
operator int();
operator struct RGB555();
};
struct RGB555 {
char red;
char green;
char blue;
RGB555(char red, char green, char blue);
RGB555(short color);
operator short();
operator struct RGBA8888();
};