Created
April 29, 2013 00:13
-
-
Save pjc0247/5478979 to your computer and use it in GitHub Desktop.
Merona - a Simple PS VITA Game Framework Prototype
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Threading; | |
using System.Diagnostics; | |
using Sce.PlayStation.Core; | |
using Sce.PlayStation.Core.Graphics; | |
using Sce.PlayStation.Core.Imaging; | |
using Sce.PlayStation.Core.Environment; | |
using Sce.PlayStation.Core.Input; | |
using System.IO; | |
using System.Reflection; | |
namespace Merona | |
{ | |
public class Font | |
{ | |
private Sce.PlayStation.Core.Imaging.Font font; | |
private Sprite sprite; | |
private UInt32 argb; | |
public Font (FontAlias font,int size,FontStyle style = FontStyle.Regular) | |
{ | |
this.font = new Sce.PlayStation.Core.Imaging.Font(font,size,style); | |
this.sprite = new Sprite(null,1,1); | |
if(this.font == null) | |
Console.WriteLine("cannot load font"); | |
setColor (0xffffffff); | |
} | |
~Font(){ | |
dispose (); | |
} | |
public void dispose(){ | |
font.Dispose(); | |
sprite.dispose (); | |
} | |
public void setColor(UInt32 color){ | |
this.argb = color; | |
} | |
public void draw(int x,int y,string text){ | |
int width = font.GetTextWidth(text, 0, text.Length); | |
int height = font.Metrics.Height; | |
width = Math.Min( width, 2048 ); | |
var image = new Image(ImageMode.Rgba, | |
new ImageSize(width, height), | |
new ImageColor(0, 0, 0, 0) ); | |
image.DrawText(text, | |
new ImageColor((int)((argb >> 16) & 0xff), | |
(int)((argb >> 8) & 0xff), | |
(int)((argb >> 0) & 0xff), | |
(int)((argb >> 24) & 0xff)), | |
font, new ImagePosition(0, 0)); | |
var texture = new Texture2D(width, height, false, PixelFormat.Rgba); | |
texture.SetPixels(0, image.ToBuffer(), 0, 0, width, height); | |
sprite.setTexture(texture); | |
sprite.draw (x,y); | |
sprite.dispose (); | |
} | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
using System.Collections.Generic; | |
using Sce.PlayStation.Core; | |
using Sce.PlayStation.Core.Environment; | |
using Sce.PlayStation.Core.Graphics; | |
using Sce.PlayStation.Core.Input; | |
using Sce.PlayStation.Core.Imaging; | |
using System.IO; | |
using System.Reflection; | |
using Merona; | |
namespace Merona | |
{ | |
public class SpriteBlendMode | |
{ | |
public static int None = 0; | |
public static int Blend = 1; | |
public static int Add = 2; | |
public static int Mod = 3; | |
} | |
public class Sprite | |
{ | |
public Texture2D texture = null; | |
public int stepW,stepH; | |
public int width,height; | |
public int step; | |
public int alpha; | |
public bool repeat; | |
public int blend; | |
public int stepMin,stepMax; | |
private VertexBuffer vertices; | |
private static ShaderProgram shaderCurrent = null; | |
private static ShaderProgram shaderTexture = null; | |
private static int idWVP; | |
private static GraphicsContext graphic = null; | |
public Sprite (string path,int w=1,int h=1) | |
{ | |
Console.WriteLine("LoadTexture : " + path + " / " + w.ToString () + "," + h.ToString ()); | |
if(graphic == null){ | |
graphic = Core.getGraphic(); | |
createSimpleTextureShader (); | |
shaderTexture = new ShaderProgram( "/Application/shaders/Texture.cgx" ); | |
shaderTexture.SetAttributeBinding( 0, "a_Position" ); | |
shaderTexture.SetAttributeBinding( 1, "a_TexCoord" ); | |
idWVP = shaderTexture.FindUniform( "WorldViewProj" ); | |
shaderCurrent = shaderTexture; | |
} | |
if(path != null){ | |
texture = new Texture2D("/Application/resource/" + path,false); | |
if(texture == null){ | |
Console.WriteLine("texture load failed : " + path); | |
} | |
} | |
this.stepW = w; | |
this.stepH = h; | |
this.step = 0; | |
this.stepMin = 0; | |
this.stepMax = w * h; | |
this.repeat = true; | |
this.alpha = 255; | |
this.blend = SpriteBlendMode.Blend; | |
if(texture != null){ | |
this.width = texture.Width; | |
this.height = texture.Height; | |
setTexture (texture); | |
} | |
else { | |
this.width = 0; | |
this.height = 0; | |
} | |
} | |
~Sprite() | |
{ | |
//dispose (); | |
} | |
public void dispose() | |
{ | |
texture.Dispose(); | |
vertices.Dispose(); | |
} | |
public void setRange(int min,int max){ | |
this.stepMin = min; | |
this.stepMax = max; | |
} | |
public void setTexture(Texture2D tex){ | |
this.texture = tex; | |
this.width = tex.Width; | |
this.height = tex.Height; | |
float l = 0; | |
float t = 0; | |
float r = texture.Width; | |
float b = texture.Height; | |
vertices = new VertexBuffer(4, VertexFormat.Float3, VertexFormat.Float2); | |
vertices.SetVertices(0, new float[]{l, t, 0, | |
r, t, 0, | |
r, b, 0, | |
l, b, 0}); | |
vertices.SetVertices(1, new float[]{0.0f, 0.0f, | |
1.0f, 0.0f, | |
1.0f, 1.0f, | |
0.0f, 1.0f}); | |
setStep (0); | |
} | |
public void setStep(int n){ | |
float unitW,unitH; | |
step = n; | |
unitW = texture.Width / this.stepW; | |
unitH = texture.Height / this.stepH; | |
SetDrawRect( | |
(float)unitW * (float)(step % stepW), | |
(float)unitH * (float)(step / stepW), | |
unitW, | |
unitH); | |
} | |
public void doStep(){ | |
step ++; | |
if(step >= stepMax){ | |
if(repeat == true){ | |
step = stepMin; | |
} | |
else{ | |
step--; | |
} | |
} | |
setStep (step); | |
} | |
public void draw(int x, int y,float scaleX=1,float scaleY=1,float degree=0) | |
{ | |
var modelMatrix = CreateModelMatrix(x,y); | |
var worldViewProj = Core.getProjectionMatrix() * Core.getViewMatrix() * modelMatrix; | |
shaderCurrent.SetUniformValue(0, ref worldViewProj); | |
int alphaRateId = shaderCurrent.FindUniform("AlphaRate"); | |
shaderCurrent.SetUniformValue(alphaRateId, (float)(alpha / 255.0f)); | |
graphic.SetShaderProgram(shaderCurrent); | |
graphic.SetVertexBuffer(0, vertices); | |
graphic.SetTexture(0, texture); | |
if(blend == SpriteBlendMode.None){ | |
graphic.Disable(EnableMode.Blend); | |
} | |
else if(blend == SpriteBlendMode.Blend){ | |
graphic.Enable(EnableMode.Blend); | |
graphic.SetBlendFunc(BlendFuncMode.Add, BlendFuncFactor.SrcAlpha, BlendFuncFactor.OneMinusSrcAlpha); | |
} | |
else if(blend == SpriteBlendMode.Add){ | |
graphic.Enable(EnableMode.Blend); | |
graphic.SetBlendFunc(BlendFuncMode.Add, BlendFuncFactor.SrcAlpha, BlendFuncFactor.One); | |
} | |
graphic.DrawArrays(DrawMode.TriangleFan, 0, 4); | |
} | |
public void SetDrawRect(float x, float y, float w, float h) | |
{ | |
vertices = new VertexBuffer(4, VertexFormat.Float3, VertexFormat.Float2); | |
vertices.SetVertices(0, new float[]{0, 0, 0, | |
w, 0, 0, | |
w, h, 0, | |
0, h, 0}); | |
float l = x / texture.Width; | |
float t = y / texture.Height; | |
float r = (x + w) / texture.Width; | |
float b = (y + h) / texture.Height; | |
vertices.SetVertices(1, new float[]{l, t, | |
r, t, | |
r, b, | |
l, b}); | |
} | |
public Matrix4 CreateModelMatrix(int x,int y,float sx=1,float sy=1,float degree=0) | |
{ | |
var centerMatrix = Matrix4.Translation(0, 0, 0.0f); | |
var transMatrix = Matrix4.Translation(x, y, 0.0f); | |
var rotMatrix = Matrix4.RotationZ((float)(degree / 180.0f * Math.PI)); | |
var scaleMatrix = Matrix4.Scale(new Vector3(sx, sy, 1.0f)); | |
return transMatrix * rotMatrix * scaleMatrix * centerMatrix; | |
} | |
private static ShaderProgram createSimpleTextureShader() | |
{ | |
//var shaderProgram = new ShaderProgram(dataBufferVertex); | |
var shaderProgram = new ShaderProgram("/Application/shaders/Texture.cgx"); | |
shaderProgram.SetAttributeBinding(0, "a_Position"); | |
shaderProgram.SetAttributeBinding(1, "a_TexCoord"); | |
shaderProgram.SetUniformBinding(0, "WorldViewProj"); | |
return shaderProgram; | |
} | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
using System.Collections.Generic; | |
using Sce.PlayStation.Core; | |
using Sce.PlayStation.Core.Environment; | |
using Sce.PlayStation.Core.Graphics; | |
using Sce.PlayStation.Core.Input; | |
using Sce.PlayStation.Core.Imaging; | |
namespace Merona | |
{ | |
public static class Core | |
{ | |
private static GraphicsContext graphic = null; | |
private static Matrix4 projection,view; | |
public static int width,height; | |
public static void Init(int w,int h){ | |
width = w; | |
height = h; | |
projection = Matrix4.Ortho(0, width, | |
0, height, | |
0.0f, 32768.0f); | |
view = Matrix4.LookAt(new Vector3(0, height, 0), | |
new Vector3(0, height, 1), | |
new Vector3(0, -1, 0)); | |
} | |
public static void setGraphic(GraphicsContext g){ | |
graphic = g; | |
} | |
public static GraphicsContext getGraphic(){ | |
return graphic; | |
} | |
public static Matrix4 getProjectionMatrix(){ | |
return projection; | |
} | |
public static Matrix4 getViewMatrix(){ | |
return view; | |
} | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Sce.PlayStation.Core; | |
using Sce.PlayStation.Core.Environment; | |
using Sce.PlayStation.Core.Graphics; | |
using Sce.PlayStation.Core.Input; | |
using Sce.PlayStation.Core.Imaging; | |
namespace Merona | |
{ | |
public class Timer | |
{ | |
public int interval; | |
private int st; | |
private bool started; | |
public static int getTicks(){ | |
return Environment.TickCount; | |
} | |
public Timer (int interval) | |
{ | |
this.interval = interval; | |
this.st = getTicks(); | |
this.started = false; | |
} | |
public void start(){ | |
this.st = getTicks (); | |
this.started = true; | |
} | |
public void stop(){ | |
this.started = false; | |
} | |
public bool done(){ | |
if(getTicks () - st >= interval){ | |
st = getTicks (); | |
return true; | |
} | |
else return false; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment