@ -0,0 +1,30 @@
|
||||
set(external_SCRIPTS |
||||
makeres.py |
||||
render_svg.py |
||||
create_resource_dat.py |
||||
) |
||||
|
||||
set(external_SOURCES |
||||
Cursor_Base.svg |
||||
Linking_Book.svg |
||||
Loading_Text_rasterfont.svg |
||||
Voice_Chat.svg |
||||
) |
||||
|
||||
if(PLASMA_EXTERNAL_RELEASE) |
||||
set(Make_Resource_Command |
||||
python ${CMAKE_CURRENT_SOURCE_DIR}/makeres.py --optimize --render --package -i ${CMAKE_CURRENT_SOURCE_DIR} -o ${CMAKE_CURRENT_BINARY_DIR}) |
||||
else(PLASMA_EXTERNAL_RELEASE) |
||||
set(Make_Resource_Command |
||||
python ${CMAKE_CURRENT_SOURCE_DIR}/makeres.py --render --package -i ${CMAKE_CURRENT_SOURCE_DIR} -o ${CMAKE_CURRENT_BINARY_DIR}) |
||||
endif(PLASMA_EXTERNAL_RELEASE) |
||||
|
||||
add_custom_command( |
||||
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/resource.dat |
||||
COMMAND ${Make_Resource_Command} |
||||
DEPENDS ${external_SOURCES} ${external_SCRIPTS} |
||||
) |
||||
add_custom_target(externalResources DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/resource.dat) |
||||
|
||||
source_group("Source Files" FILES ${external_SOURCES}) |
||||
source_group("Script Files" FILES ${external_SCRIPTS}) |
After Width: | Height: | Size: 14 KiB |
After Width: | Height: | Size: 20 KiB |
After Width: | Height: | Size: 22 KiB |
After Width: | Height: | Size: 48 KiB |
@ -0,0 +1,62 @@
|
||||
#!/usr/bin/env python |
||||
|
||||
from __future__ import print_function |
||||
from __future__ import with_statement |
||||
|
||||
import os |
||||
import sys |
||||
import glob |
||||
import struct |
||||
from optparse import OptionParser |
||||
|
||||
version = 1 |
||||
|
||||
def create_resource_dat(resfilepath, inrespath): |
||||
datHeader = 0xCBBCF00D |
||||
datVersion = 0x00000001 |
||||
|
||||
## Get list of files to archive |
||||
resourceList = glob.glob(os.path.join(inrespath, "*")) |
||||
resourceList.sort() |
||||
if len(resourceList) == 0: |
||||
print("No files found in '{0}'. Quitting.\n".format(inrespath)) |
||||
return False |
||||
print("{0} resources found in '{1}'.".format(len(resourceList), inrespath, )) |
||||
|
||||
## Write each resource into the output file |
||||
with open(resfilepath, "wb") as datFile: |
||||
datFile.write(struct.pack("<I",datHeader)) |
||||
datFile.write(struct.pack("<I",datVersion)) |
||||
datFile.write(struct.pack("<I",len(resourceList))) |
||||
for res in resourceList: |
||||
with open(res, "rb") as resFile: |
||||
name = os.path.basename(res) |
||||
datFile.write(struct.pack("<I", len(name))) |
||||
datFile.write(name) |
||||
datFile.write(struct.pack("<I", os.path.getsize(res))) |
||||
datFile.write(resFile.read()) |
||||
|
||||
print("{0} resources written to '{1}'.\n".format(len(resourceList), resfilepath)) |
||||
|
||||
return True |
||||
|
||||
if __name__ == '__main__': |
||||
parser = OptionParser(usage="usage: %prog [options]", version="%prog {0}".format(version)) |
||||
parser.add_option("-q", "--quiet", dest="verbose", default=True, action="store_false", help="Don't print status messages") |
||||
parser.add_option("-o", "--outfile", dest="outfile", default="resource.dat", help="Sets name for output file") |
||||
parser.add_option("-i", "--inpath", dest="inpath", default=".", help="Sets input path for files to add to resource file") |
||||
|
||||
(options, args) = parser.parse_args() |
||||
|
||||
## Send output to OS's null if unwanted |
||||
if not options.verbose: |
||||
sys.stdout = open(os.devnull,"w") |
||||
sys.stderr = open(os.devnull,"w") |
||||
|
||||
## Compute Paths |
||||
outfile = os.path.expanduser(options.outfile) |
||||
inpath = os.path.expanduser(options.inpath) |
||||
|
||||
## Do the work! |
||||
print("Creating {0}...".format(outfile)) |
||||
create_resource_dat(outfile, inpath) |
@ -0,0 +1,52 @@
|
||||
#!/usr/bin/env python |
||||
|
||||
import os |
||||
import sys |
||||
import glob |
||||
import subprocess |
||||
from optparse import OptionParser |
||||
|
||||
|
||||
if __name__ == '__main__': |
||||
parser = OptionParser(usage="usage: %prog [options]") |
||||
parser.add_option("-q", "--quiet", dest="verbose", default=True, action="store_false", help="Don't print status messages") |
||||
parser.add_option("-r", "--render", dest="render", default=False, action="store_true", help="Perform SVG Render to images") |
||||
parser.add_option("-p", "--package", dest="package", default=False, action="store_true", help="Perform packaging into resource container") |
||||
parser.add_option("-z", "--optimize", dest="optimize", default=False, action="store_true", help="Perform PNGCrush optimization on PNG resources") |
||||
parser.add_option("-o", "--outpath", dest="outpath", default=".", help="Sets output path for resource container") |
||||
parser.add_option("-i", "--inpath", dest="inpath", default=".", help="Sets input path for files to add to resource file") |
||||
|
||||
(options, args) = parser.parse_args() |
||||
|
||||
## Send output to OS's null if unwanted |
||||
if not options.verbose: |
||||
sys.stdout = open(os.devnull,"w") |
||||
sys.stderr = open(os.devnull,"w") |
||||
|
||||
## Compute Paths |
||||
outpath = os.path.expanduser(options.outpath) |
||||
inpath = os.path.expanduser(options.inpath) |
||||
|
||||
## Do the work! |
||||
if options.render: |
||||
ret = subprocess.call(["python", os.path.join(inpath, "render_svg.py"), "-i", inpath, "-o", os.path.join(outpath, "render")], stdout=sys.stdout, stderr=sys.stderr) |
||||
if ret != 0: |
||||
print("An error has occurred. Aborting.") |
||||
exit(1) |
||||
|
||||
if options.optimize: |
||||
print("Optimizing PNGs with pngcrush...") |
||||
for png in glob.glob(os.path.join("render", "*.png")): |
||||
#print("pngcrushing - {0}".format(png)) |
||||
ret = subprocess.call(["pngcrush", "-q", "-l 9", "-brute", png, "temp.png"], stdout=sys.stdout, stderr=sys.stderr) |
||||
if ret != 0: |
||||
print("An error has occurred. Aborting.") |
||||
exit(1) |
||||
os.remove(png) |
||||
os.rename("temp.png", png) |
||||
|
||||
if options.package: |
||||
ret = subprocess.call(["python", os.path.join(inpath, "create_resource_dat.py"), "-i", os.path.join(outpath, "render"), "-o", "resource.dat"], stdout=sys.stdout, stderr=sys.stderr) |
||||
if ret != 0: |
||||
print("An error has occurred. Aborting.") |
||||
exit(1) |
@ -0,0 +1,205 @@
|
||||
#!/usr/bin/env python |
||||
|
||||
from __future__ import print_function |
||||
from __future__ import with_statement |
||||
|
||||
|
||||
import os |
||||
import math |
||||
from xml.dom.minidom import parse |
||||
from optparse import OptionParser |
||||
|
||||
try: |
||||
import rsvg |
||||
import cairo |
||||
except ImportError as e: |
||||
print("Rendering SVG resources requires PyGTK. Exiting...") |
||||
exit(1) |
||||
|
||||
cursorList = { |
||||
"cursor_up": ["circleOuter"], |
||||
"cursor_poised": ["circleOuter", "circleInnerOpen"], |
||||
"cursor_clicked": ["circleOuter", "circleInnerClosed"], |
||||
"cursor_disabled": ["circleOuter", "cross"], |
||||
|
||||
"cursor_open": ["circleOuter", "arrowGreyUpper", "arrowGreyLower"], |
||||
"cursor_grab": ["circleOuter", "circleInnerClosed", "arrowGreyUpper", "arrowGreyLower"], |
||||
"cursor_updown_open": ["circleOuter", "circleInnerClosed", "arrowGreyUpper", "arrowGreyLower"], |
||||
"cursor_updown_closed": ["circleOuter", "circleInnerClosed", "arrowWhiteUpper", "arrowWhiteLower"], |
||||
|
||||
"cursor_leftright_open": ["circleOuter", "circleInnerClosed", "arrowGreyRight", "arrowGreyLeft"], |
||||
"cursor_leftright_closed": ["circleOuter", "circleInnerClosed", "arrowWhiteRight", "arrowWhiteLeft"], |
||||
|
||||
"cursor_4way_open": ["circleOuter", "circleInnerClosed", "arrowGreyUpper", "arrowGreyRight", "arrowGreyLower", "arrowGreyLeft"], |
||||
"cursor_4way_closed": ["circleOuter", "circleInnerClosed", "arrowWhiteUpper", "arrowWhiteRight", "arrowWhiteLower", "arrowWhiteLeft"], |
||||
|
||||
"cursor_upward": ["circleOuter", "arrowWhiteUpper"], |
||||
"cursor_right": ["circleOuter", "arrowWhiteRight"], |
||||
"cursor_down": ["circleOuter", "arrowWhiteLower"], |
||||
"cursor_left": ["circleOuter", "arrowWhiteLeft"], |
||||
|
||||
"cursor_book": ["circleOuter", "book"], |
||||
"cursor_book_poised": ["circleOuter", "circleInnerOpen", "book"], |
||||
"cursor_book_clicked": ["circleOuter", "circleInnerClosed", "book"], |
||||
} |
||||
cursorOffsetList = { |
||||
"book": [8, 8] |
||||
} |
||||
|
||||
textList = { |
||||
"xLoading_Linking_Text": ["background", "circles", "textLinking"], |
||||
"xLoading_Updating_Text": ["background", "circles", "textUpdating"] |
||||
} |
||||
|
||||
voiceList = { |
||||
"ui_speaker": ["speakerGrille", "speakerIndicator", "speakerOuterRing"], |
||||
"ui_microphone": ["microphoneGrille", "microphoneIndicator", "microphoneOuterRing"] |
||||
} |
||||
|
||||
def enable_only_layers(layerlist, layers): |
||||
for layer in layers: |
||||
if layer in layerlist: |
||||
layers[layer].setAttribute("style","") |
||||
else: |
||||
layers[layer].setAttribute("style","display:none") |
||||
|
||||
def get_layers_from_svg(svgData): |
||||
inkscapeNS = "http://www.inkscape.org/namespaces/inkscape" |
||||
layers = {} |
||||
|
||||
groups = svgData.getElementsByTagName("g") |
||||
for group in groups: |
||||
if group.getAttributeNS(inkscapeNS,"groupmode") == "layer": |
||||
layers[group.getAttribute("id")] = group |
||||
|
||||
return layers |
||||
|
||||
def render_cursors(inpath, outpath): |
||||
resSize = {"width":32, "height":32} |
||||
with open(os.path.join(inpath,"Cursor_Base.svg"), "r") as svgFile: |
||||
cursorSVG = parse(svgFile) |
||||
layers = get_layers_from_svg(cursorSVG) |
||||
ratioW = resSize["width"] / float(cursorSVG.documentElement.getAttribute("width")) |
||||
ratioH = resSize["height"] / float(cursorSVG.documentElement.getAttribute("height")) |
||||
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, resSize["width"], resSize["height"]) |
||||
|
||||
for cursor in cursorList: |
||||
ctx = cairo.Context(surface) |
||||
ctx.save() |
||||
ctx.set_operator(cairo.OPERATOR_CLEAR) |
||||
ctx.paint() |
||||
ctx.restore() |
||||
|
||||
enable_only_layers(cursorList[cursor], layers) |
||||
|
||||
for layerName in cursorOffsetList: |
||||
if layerName in cursorList[cursor]: |
||||
ctx.translate(*cursorOffsetList[layerName]) |
||||
svg = rsvg.Handle(data=cursorSVG.toxml()) |
||||
ctx.scale(ratioW, ratioH) |
||||
svg.render_cairo(ctx) |
||||
|
||||
surface.write_to_png(os.path.join(outpath, cursor + ".png")) |
||||
|
||||
def render_loading_books(inpath, outpath): |
||||
resSize = {"width":256, "height":256} |
||||
with open(os.path.join(inpath,"Linking_Book.svg"), "r") as svgFile: |
||||
bookSVG = parse(svgFile) |
||||
layers = get_layers_from_svg(bookSVG) |
||||
ratioW = resSize["width"] / float(bookSVG.documentElement.getAttribute("width")) |
||||
ratioH = resSize["height"] / float(bookSVG.documentElement.getAttribute("height")) |
||||
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, resSize["width"], resSize["height"]) |
||||
|
||||
for angle in range(0, 18): |
||||
ctx = cairo.Context(surface) |
||||
|
||||
# Draw Book and Black Background |
||||
enable_only_layers(["background", "book"],layers) |
||||
svg = rsvg.Handle(data=bookSVG.toxml()) |
||||
ctx.save() |
||||
ctx.scale(ratioW, ratioH) |
||||
svg.render_cairo(ctx) |
||||
ctx.restore() |
||||
|
||||
# Draw Circles at appropriate angle |
||||
enable_only_layers(["circles"],layers) |
||||
svg = rsvg.Handle(data=bookSVG.toxml()) |
||||
ctx.translate(resSize["height"] / 2, resSize["width"] / 2) |
||||
ctx.rotate(math.radians(angle*(5))) |
||||
ctx.translate(-resSize["width"] / 2, -resSize["height"] / 2) |
||||
ctx.scale(ratioW, ratioH) |
||||
svg.render_cairo(ctx) |
||||
|
||||
surface.write_to_png(os.path.join(outpath, "xLoading_Linking.{0:02}.png".format(angle))) |
||||
|
||||
def render_loading_text(inpath, outpath): |
||||
resSize = {"width":192, "height":41} |
||||
with open(os.path.join(inpath,"Loading_Text_rasterfont.svg"), "r") as svgFile: |
||||
textSVG = parse(svgFile) |
||||
layers = get_layers_from_svg(textSVG) |
||||
ratioW = resSize["width"] / float(textSVG.documentElement.getAttribute("width")) |
||||
ratioH = resSize["height"] / float(textSVG.documentElement.getAttribute("height")) |
||||
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, resSize["width"], resSize["height"]) |
||||
|
||||
for textEntry in textList: |
||||
ctx = cairo.Context(surface) |
||||
ctx.save() |
||||
ctx.set_operator(cairo.OPERATOR_CLEAR) |
||||
ctx.paint() |
||||
ctx.restore() |
||||
enable_only_layers(textList[textEntry], layers) |
||||
svg = rsvg.Handle(data=textSVG.toxml()) |
||||
ctx.scale(ratioW, ratioH) |
||||
svg.render_cairo(ctx) |
||||
surface.write_to_png(os.path.join(outpath, textEntry + ".png")) |
||||
|
||||
def render_voice_icons(inpath, outpath): |
||||
resSize = {"width":32, "height":32} |
||||
with open(os.path.join(inpath,"Voice_Chat.svg"), "r") as svgFile: |
||||
uiSVG = parse(svgFile) |
||||
layers = get_layers_from_svg(uiSVG) |
||||
ratioW = resSize["width"] / float(uiSVG.documentElement.getAttribute("width")) |
||||
ratioH = resSize["height"] / float(uiSVG.documentElement.getAttribute("height")) |
||||
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, resSize["width"], resSize["height"]) |
||||
|
||||
for voiceUI in voiceList: |
||||
ctx = cairo.Context(surface) |
||||
ctx.save() |
||||
ctx.set_operator(cairo.OPERATOR_CLEAR) |
||||
ctx.paint() |
||||
ctx.restore() |
||||
|
||||
enable_only_layers(voiceList[voiceUI], layers) |
||||
|
||||
svg = rsvg.Handle(data=uiSVG.toxml()) |
||||
ctx.scale(ratioW, ratioH) |
||||
svg.render_cairo(ctx) |
||||
|
||||
surface.write_to_png(os.path.join(outpath, voiceUI + ".png")) |
||||
|
||||
if __name__ == '__main__': |
||||
parser = OptionParser(usage="usage: %prog [options]") |
||||
parser.add_option("-q", "--quiet", dest="verbose", default=True, action="store_false", help="Don't print status messages") |
||||
parser.add_option("-o", "--outpath", dest="outpath", default="./out", help="Sets output path for rendered images") |
||||
parser.add_option("-i", "--inpath", dest="inpath", default=".", help="Sets input path for SVG files") |
||||
|
||||
(options, args) = parser.parse_args() |
||||
|
||||
## Send output to OS's null if unwanted |
||||
if not options.verbose: |
||||
sys.stdout = open(os.devnull,"w") |
||||
sys.stderr = open(os.devnull,"w") |
||||
|
||||
## Compute Paths |
||||
outpath = os.path.expanduser(options.outpath) |
||||
inpath = os.path.expanduser(options.inpath) |
||||
|
||||
if not os.path.exists(outpath): |
||||
os.mkdir(outpath) |
||||
|
||||
## Do the work! |
||||
print("Rendering SVGs...") |
||||
render_cursors(inpath, outpath) |
||||
render_loading_books(inpath, outpath) |
||||
render_loading_text(inpath, outpath) |
||||
render_voice_icons(inpath, outpath) |
Before Width: | Height: | Size: 630 B |
Before Width: | Height: | Size: 630 B |
Before Width: | Height: | Size: 630 B |
Before Width: | Height: | Size: 630 B |
Before Width: | Height: | Size: 630 B |
Before Width: | Height: | Size: 630 B |
Before Width: | Height: | Size: 630 B |
Before Width: | Height: | Size: 630 B |
Before Width: | Height: | Size: 2.1 KiB |
Before Width: | Height: | Size: 630 B |
Before Width: | Height: | Size: 630 B |
Before Width: | Height: | Size: 630 B |
Before Width: | Height: | Size: 630 B |
Before Width: | Height: | Size: 630 B |
Before Width: | Height: | Size: 630 B |
Before Width: | Height: | Size: 630 B |
Before Width: | Height: | Size: 630 B |
Before Width: | Height: | Size: 630 B |
Before Width: | Height: | Size: 630 B |
Before Width: | Height: | Size: 630 B |
Before Width: | Height: | Size: 630 B |
Before Width: | Height: | Size: 2.1 KiB |
Before Width: | Height: | Size: 2.1 KiB |
Before Width: | Height: | Size: 630 B |
Before Width: | Height: | Size: 6.4 KiB |
Before Width: | Height: | Size: 6.3 KiB |
Before Width: | Height: | Size: 6.3 KiB |
Before Width: | Height: | Size: 6.4 KiB |
Before Width: | Height: | Size: 6.3 KiB |
Before Width: | Height: | Size: 6.4 KiB |
Before Width: | Height: | Size: 6.3 KiB |
Before Width: | Height: | Size: 6.4 KiB |
Before Width: | Height: | Size: 6.3 KiB |
Before Width: | Height: | Size: 6.3 KiB |
Before Width: | Height: | Size: 6.3 KiB |
Before Width: | Height: | Size: 6.4 KiB |
Before Width: | Height: | Size: 6.2 KiB |
Before Width: | Height: | Size: 6.4 KiB |
Before Width: | Height: | Size: 6.3 KiB |
Before Width: | Height: | Size: 6.3 KiB |
Before Width: | Height: | Size: 6.3 KiB |
Before Width: | Height: | Size: 6.4 KiB |
Before Width: | Height: | Size: 29 KiB |
Before Width: | Height: | Size: 29 KiB |
@ -0,0 +1,17 @@
|
||||
include_directories("../../CoreLib") |
||||
include_directories("../../NucleusLib/inc") |
||||
include_directories("../../NucleusLib") |
||||
include_directories("../../PubUtilLib") |
||||
|
||||
set(plClientResMgr_SOURCES |
||||
plClientResMgr.cpp |
||||
) |
||||
|
||||
set(plClientResMgr_HEADERS |
||||
plClientResMgr.h |
||||
) |
||||
|
||||
add_library(plClientResMgr STATIC ${plClientResMgr_SOURCES} ${plClientResMgr_HEADERS}) |
||||
|
||||
source_group("Source Files" FILES ${plClientResMgr_SOURCES}) |
||||
source_group("Header Files" FILES ${plClientResMgr_HEADERS}) |
@ -0,0 +1,132 @@
|
||||
/*==LICENSE==*
|
||||
|
||||
CyanWorlds.com Engine - MMOG client, server and tools |
||||
Copyright (C) 2011 Cyan Worlds, Inc. |
||||
|
||||
This program is free software: you can redistribute it and/or modify |
||||
it under the terms of the GNU General Public License as published by |
||||
the Free Software Foundation, either version 3 of the License, or |
||||
(at your option) any later version. |
||||
|
||||
This program is distributed in the hope that it will be useful, |
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
GNU General Public License for more details. |
||||
|
||||
You should have received a copy of the GNU General Public License |
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
You can contact Cyan Worlds, Inc. by email legal@cyan.com |
||||
or by snail mail at: |
||||
Cyan Worlds, Inc. |
||||
14617 N Newport Hwy |
||||
Mead, WA 99021 |
||||
|
||||
*==LICENSE==*/ |
||||
|
||||
#include "hsTypes.h" |
||||
#include "hsUtils.h" |
||||
#include "hsStream.h" |
||||
#include "hsResMgr.h" |
||||
#include "plJPEG/plJPEG.h" |
||||
#include "plGImage/plPNG.h" |
||||
#include "plGImage/plMipmap.h" |
||||
|
||||
#include "plClientResMgr.h" |
||||
|
||||
|
||||
//// Singleton Instance ///////////////////////////////////////////////////////
|
||||
|
||||
plClientResMgr& plClientResMgr::Instance(void) |
||||
{ |
||||
static plClientResMgr theInstance; |
||||
return theInstance; |
||||
} |
||||
|
||||
plClientResMgr::plClientResMgr() |
||||
{ |
||||
this->ClientResources = TRACKED_NEW std::map<std::string, plMipmap*>; |
||||
} |
||||
|
||||
plClientResMgr::~plClientResMgr() |
||||
{ |
||||
if (this->ClientResources) { |
||||
std::map<std::string, plMipmap*>::iterator it; |
||||
|
||||
for (it = this->ClientResources->begin(); it != this->ClientResources->end(); ++it) { |
||||
it->second->UnRef(); |
||||
} |
||||
|
||||
delete this->ClientResources; |
||||
} |
||||
} |
||||
|
||||
void plClientResMgr::ILoadResources(const char* resfile) |
||||
{ |
||||
if (!resfile) { |
||||
return; |
||||
} |
||||
|
||||
wchar* wFilename = hsStringToWString(resfile); |
||||
hsUNIXStream in; |
||||
|
||||
if (in.Open(wFilename, L"rb")) { |
||||
UInt32 header = in.ReadSwap32(); |
||||
UInt32 version = in.ReadSwap32(); |
||||
UInt32 num_resources = 0; |
||||
|
||||
switch (version) { |
||||
case 1: |
||||
num_resources = in.ReadSwap32(); |
||||
|
||||
for (int i = 0; i < num_resources; i++) { |
||||
plMipmap* res_data = NULL; |
||||
UInt32 res_size = 0; |
||||
char* tmp_name = in.ReadSafeStringLong(); |
||||
std::string res_name = std::string(tmp_name); |
||||
std::string res_type = res_name.substr(res_name.length() - 4, 4); |
||||
delete tmp_name; |
||||
|
||||
// Version 1 doesn't encode format, so we'll try some simple
|
||||
// extension sniffing
|
||||
if (res_type == ".png") { |
||||
// Read resource stream size, but the PNG has that info in the header
|
||||
// so it's not needed
|
||||
res_size = in.ReadSwap32(); |
||||
res_data = plPNG::Instance().ReadFromStream(&in); |
||||
} else if (res_type == ".jpg") { |
||||
// Don't read resource stream size, as plJPEG's reader will need it
|
||||
res_data = plJPEG::Instance().ReadFromStream(&in); |
||||
} else { |
||||
// Original Myst5 format only is known to support Targa,
|
||||
// so default fallback is targa
|
||||
// TODO - Add plTarga::ReadFromStream()
|
||||
} |
||||
|
||||
(*this->ClientResources)[res_name] = res_data; |
||||
} |
||||
|
||||
break; |
||||
default: |
||||
break; |
||||
} |
||||
|
||||
in.Close(); |
||||
} |
||||
|
||||
delete wFilename; |
||||
} |
||||
|
||||
plMipmap* plClientResMgr::getResource(const char* resname) |
||||
{ |
||||
plMipmap* resmipmap = NULL; |
||||
std::map<std::string, plMipmap*>::iterator it = this->ClientResources->find(resname); |
||||
|
||||
if (it != this->ClientResources->end()) { |
||||
resmipmap = it->second; |
||||
} else { |
||||
hsAssert(resmipmap, "Unknown client resource requested."); |
||||
} |
||||
|
||||
return resmipmap; |
||||
} |
@ -0,0 +1,50 @@
|
||||
/*==LICENSE==*
|
||||
|
||||
CyanWorlds.com Engine - MMOG client, server and tools |
||||
Copyright (C) 2011 Cyan Worlds, Inc. |
||||
|
||||
This program is free software: you can redistribute it and/or modify |
||||
it under the terms of the GNU General Public License as published by |
||||
the Free Software Foundation, either version 3 of the License, or |
||||
(at your option) any later version. |
||||
|
||||
This program is distributed in the hope that it will be useful, |
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
GNU General Public License for more details. |
||||
|
||||
You should have received a copy of the GNU General Public License |
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
You can contact Cyan Worlds, Inc. by email legal@cyan.com |
||||
or by snail mail at: |
||||
Cyan Worlds, Inc. |
||||
14617 N Newport Hwy |
||||
Mead, WA 99021 |
||||
|
||||
*==LICENSE==*/ |
||||
|
||||
#ifndef _plClientResMgr_h |
||||
#define _plClientResMgr_h |
||||
|
||||
#include <map> |
||||
#include <string> |
||||
|
||||
class plMipmap; |
||||
|
||||
class plClientResMgr { |
||||
protected: |
||||
std::map<std::string, plMipmap*>* ClientResources; |
||||
|
||||
public: |
||||
plClientResMgr(); |
||||
~plClientResMgr(); |
||||
|
||||
void ILoadResources(const char* resfile); |
||||
|
||||
plMipmap* getResource(const char* resname); |
||||
|
||||
static plClientResMgr& Instance(void); |
||||
}; |
||||
|
||||
#endif // _plClientResMgr_
|
@ -0,0 +1,254 @@
|
||||
/*==LICENSE==*
|
||||
|
||||
CyanWorlds.com Engine - MMOG client, server and tools |
||||
Copyright (C) 2011 Cyan Worlds, Inc. |
||||
|
||||
This program is free software: you can redistribute it and/or modify |
||||
it under the terms of the GNU General Public License as published by |
||||
the Free Software Foundation, either version 3 of the License, or |
||||
(at your option) any later version. |
||||
|
||||
This program is distributed in the hope that it will be useful, |
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
GNU General Public License for more details. |
||||
|
||||
You should have received a copy of the GNU General Public License |
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
You can contact Cyan Worlds, Inc. by email legal@cyan.com |
||||
or by snail mail at: |
||||
Cyan Worlds, Inc. |
||||
14617 N Newport Hwy |
||||
Mead, WA 99021 |
||||
|
||||
*==LICENSE==*/ |
||||
|
||||
#include "hsTypes.h" |
||||
#include "hsStream.h" |
||||
#include "hsExceptions.h" |
||||
#include "hsUtils.h" |
||||
#include "plPNG.h" |
||||
#include "plGImage/plMipmap.h" |
||||
|
||||
#include <png.h> |
||||
#define PNGSIGSIZE 8 |
||||
|
||||
// Custom functions to read and write data from or to an hsStream
|
||||
// used by libPNG's respective functions
|
||||
void pngReadDelegate(png_structp png_ptr, png_bytep png_data, png_size_t length) |
||||
{ |
||||
hsStream* inStream = (hsStream*)png_get_io_ptr(png_ptr); |
||||
inStream->Read(length, (UInt8*)png_data); |
||||
} |
||||
|
||||
void pngWriteDelegate(png_structp png_ptr, png_bytep png_data, png_size_t length) |
||||
{ |
||||
hsStream* outStream = (hsStream*)png_get_io_ptr(png_ptr); |
||||
outStream->Write(length, (UInt8*)png_data); |
||||
} |
||||
|
||||
//// Singleton Instance ///////////////////////////////////////////////////////
|
||||
|
||||
plPNG& plPNG::Instance(void) |
||||
{ |
||||
static plPNG theInstance; |
||||
return theInstance; |
||||
} |
||||
|
||||
//// IRead ////////////////////////////////////////////////////////////////////
|
||||
// Given an open hsStream, reads the PNG data off of the
|
||||
// stream and decodes it into a new plMipmap. The mipmap's buffer ends up
|
||||
// being a packed RGBA buffer.
|
||||
// Returns a pointer to the new mipmap if successful, NULL otherwise.
|
||||
|
||||
plMipmap* plPNG::IRead(hsStream* inStream) |
||||
{ |
||||
plMipmap* newMipmap = NULL; |
||||
png_structp png_ptr; |
||||
png_infop info_ptr; |
||||
png_infop end_info; |
||||
|
||||
try { |
||||
// Check PNG Signature
|
||||
png_byte sig[PNGSIGSIZE]; |
||||
inStream->Read8Bytes((char*) sig); |
||||
|
||||
if (!png_sig_cmp(sig, 0, PNGSIGSIZE)) { |
||||
// Allocate required structs
|
||||
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); |
||||
|
||||
if (!png_ptr) { |
||||
throw(false); |
||||
} |
||||
|
||||
info_ptr = png_create_info_struct(png_ptr); |
||||
|
||||
if (!info_ptr) { |
||||
png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL); |
||||
throw(false); |
||||
} |
||||
|
||||
end_info = png_create_info_struct(png_ptr); |
||||
|
||||
if (!end_info) { |
||||
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL); |
||||
throw(false); |
||||
} |
||||
|
||||
// Assign delegate function for reading from hsStream
|
||||
png_set_read_fn(png_ptr, (png_voidp)inStream, pngReadDelegate); |
||||
// Get PNG Header information
|
||||
png_set_sig_bytes(png_ptr, PNGSIGSIZE); |
||||
png_read_info(png_ptr, info_ptr); |
||||
png_uint_32 imgWidth = png_get_image_width(png_ptr, info_ptr); |
||||
png_uint_32 imgHeight = png_get_image_height(png_ptr, info_ptr); |
||||
png_uint_32 bitdepth = png_get_bit_depth(png_ptr, info_ptr); |
||||
png_uint_32 channels = png_get_channels(png_ptr, info_ptr); |
||||
png_uint_32 color_type = png_get_color_type(png_ptr, info_ptr); |
||||
|
||||
// Convert images to RGB color space
|
||||
switch (color_type) { |
||||
case PNG_COLOR_TYPE_PALETTE: |
||||
png_set_palette_to_rgb(png_ptr); |
||||
channels = 3; |
||||
break; |
||||
case PNG_COLOR_TYPE_GRAY: |
||||
|
||||
if (bitdepth < 8) { |
||||
png_set_expand_gray_1_2_4_to_8(png_ptr); |
||||
} |
||||
|
||||
bitdepth = 8; |
||||
break; |
||||
} |
||||
|
||||
// Convert transparency (if needed) to a full alpha channel
|
||||
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) { |
||||
png_set_tRNS_to_alpha(png_ptr); |
||||
channels += 1; |
||||
} else if (channels == 3) { |
||||
// Add an opaque alpha channel if still none exists
|
||||
png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER); |
||||
channels = 4; |
||||
} |
||||
|
||||
// Invert color byte-order as used by plMipmap for DirectX
|
||||
png_set_bgr(png_ptr); |
||||
/// Construct a new mipmap to hold everything
|
||||
newMipmap = TRACKED_NEW plMipmap(imgWidth, imgHeight, plMipmap::kARGB32Config, 1, plMipmap::kUncompressed); |
||||
char* destp = (char*)newMipmap->GetImage(); |
||||
png_bytep* row_ptrs = TRACKED_NEW png_bytep[imgHeight]; |
||||
const unsigned int stride = imgWidth * bitdepth * channels / 8; |
||||
|
||||
// Assign row pointers to the appropriate locations in the newly-created Mipmap
|
||||
for (size_t i = 0; i < imgHeight; i++) { |
||||
row_ptrs[i] = (png_bytep)destp + (i * stride); |
||||
} |
||||
|
||||
png_read_image(png_ptr, row_ptrs); |
||||
png_read_end(png_ptr, end_info); |
||||
// Clean up allocated structs
|
||||
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); |
||||
delete [] row_ptrs; |
||||
} |
||||
} catch (...) { |
||||
if (newMipmap != NULL) { |
||||
delete newMipmap; |
||||
newMipmap = NULL; |
||||
} |
||||
} |
||||
|
||||
return newMipmap; |
||||
} |
||||
|
||||
plMipmap* plPNG::ReadFromFile(const char* fileName) |
||||
{ |
||||
wchar* wFilename = hsStringToWString(fileName); |
||||
plMipmap* retVal = ReadFromFile(wFilename); |
||||
delete [] wFilename; |
||||
return retVal; |
||||
} |
||||
|
||||
plMipmap* plPNG::ReadFromFile(const wchar* fileName) |
||||
{ |
||||
hsUNIXStream in; |
||||
|
||||
if (!in.Open(fileName, L"rb")) { |
||||
return false; |
||||
} |
||||
|
||||
plMipmap* ret = IRead(&in); |
||||
in.Close(); |
||||
return ret; |
||||
} |
||||
|
||||
hsBool plPNG::IWrite(plMipmap* source, hsStream* outStream) |
||||
{ |
||||
hsBool result = true; |
||||
|
||||
try { |
||||
// Allocate required structs
|
||||
png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); |
||||
|
||||
if (!png_ptr) { |
||||
throw(false); |
||||
} |
||||
|
||||
png_infop info_ptr = png_create_info_struct(png_ptr); |
||||
|
||||
if (!info_ptr) { |
||||
png_destroy_write_struct(&png_ptr, (png_infopp)NULL); |
||||
throw(false); |
||||
} |
||||
|
||||
// Assign delegate function for writing to hsStream
|
||||
png_set_write_fn(png_ptr, (png_voidp)outStream, pngWriteDelegate, NULL); |
||||
UInt8 psize = source->GetPixelSize(); |
||||
png_set_IHDR(png_ptr, info_ptr, source->GetWidth(), source->GetHeight(), 8, PNG_COLOR_TYPE_RGB_ALPHA, |
||||
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); |
||||
// Invert color byte-order as used by plMipmap for DirectX
|
||||
png_set_bgr(png_ptr); |
||||
// Write out the image metadata
|
||||
png_write_info(png_ptr, info_ptr); |
||||
char* srcp = (char*)source->GetImage(); |
||||
png_bytep* row_ptrs = TRACKED_NEW png_bytep[source->GetHeight()]; |
||||
const unsigned int stride = source->GetWidth() * source->GetPixelSize() / 8; |
||||
|
||||
// Assign row pointers to the appropriate locations in the newly-created Mipmap
|
||||
for (size_t i = 0; i < source->GetHeight(); i++) { |
||||
row_ptrs[i] = (png_bytep)srcp + (i * stride); |
||||
} |
||||
|
||||
png_write_image(png_ptr, row_ptrs); |
||||
png_write_end(png_ptr, info_ptr); |
||||
// Clean up allocated structs
|
||||
png_destroy_write_struct(&png_ptr, &info_ptr); |
||||
delete [] row_ptrs; |
||||
} catch (...) { |
||||
result = false; |
||||
} |
||||
|
||||
return result; |
||||
} |
||||
|
||||
hsBool plPNG::WriteToFile(const char* fileName, plMipmap* sourceData) |
||||
{ |
||||
wchar* wFilename = hsStringToWString(fileName); |
||||
hsBool retVal = WriteToFile(wFilename, sourceData); |
||||
delete [] wFilename; |
||||
return retVal; |
||||
} |
||||
|
||||
hsBool plPNG::WriteToFile(const wchar* fileName, plMipmap* sourceData) |
||||
{ |
||||
hsUNIXStream out; |
||||
|
||||
if (!out.Open(fileName, L"wb")) { |
||||
return false; |
||||
} |
||||
|
||||
hsBool ret = IWrite(sourceData, &out); |
||||
out.Close(); |
||||
return ret; |
||||
} |
@ -0,0 +1,55 @@
|
||||
/*==LICENSE==*
|
||||
|
||||
CyanWorlds.com Engine - MMOG client, server and tools |
||||
Copyright (C) 2011 Cyan Worlds, Inc. |
||||
|
||||
This program is free software: you can redistribute it and/or modify |
||||
it under the terms of the GNU General Public License as published by |
||||
the Free Software Foundation, either version 3 of the License, or |
||||
(at your option) any later version. |
||||
|
||||
This program is distributed in the hope that it will be useful, |
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
GNU General Public License for more details. |
||||
|
||||
You should have received a copy of the GNU General Public License |
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
You can contact Cyan Worlds, Inc. by email legal@cyan.com |
||||
or by snail mail at: |
||||
Cyan Worlds, Inc. |
||||
14617 N Newport Hwy |
||||
Mead, WA 99021 |
||||
|
||||
*==LICENSE==*/ |
||||
|
||||
#ifndef _plPNG_h |
||||
#define _plPNG_h |
||||
|
||||
|
||||
//// Class Definition /////////////////////////////////////////////////////////
|
||||
|
||||
class plMipmap; |
||||
class hsStream; |
||||
|
||||
class plPNG { |
||||
protected: |
||||
|
||||
plMipmap* IRead(hsStream* inStream); |
||||
hsBool IWrite(plMipmap* source, hsStream* outStream); |
||||
|
||||
public: |
||||
|
||||
plMipmap* ReadFromStream(hsStream* inStream) { return IRead(inStream); } |
||||
plMipmap* ReadFromFile(const char* fileName); |
||||
plMipmap* ReadFromFile(const wchar* fileName); |
||||
|
||||
hsBool WriteToStream(hsStream* outStream, plMipmap* sourceData) { return IWrite(sourceData, outStream); } |
||||
hsBool WriteToFile(const char* fileName, plMipmap* sourceData); |
||||
hsBool WriteToFile(const wchar* fileName, plMipmap* sourceData); |
||||
|
||||
static plPNG& Instance(void); |
||||
}; |
||||
|
||||
#endif // _plPNG_h
|