From 64fc9d91a1166f5655bef799aa6a7ecfc6b916d2 Mon Sep 17 00:00:00 2001 From: Hion-V Date: Sat, 13 Mar 2021 00:11:14 +0100 Subject: [PATCH] implemented atlas loading --- hGameTest/res/textures/sheets/sheet.htex.json | 42 ++++++++--------- .../res/textures/sheets/sheetAnim.htex.json | 32 ++++--------- hGameTest/res/textures/sprites/missing.png | Bin 0 -> 161 bytes hGameTest/src/Game.hx | 24 ++++++---- hGameTest/src/assets/HTex.hx | 26 ++++++++--- hGameTest/src/assets/TextureData.hx | 44 ------------------ hGameTest/src/assets/files/TextureFile.hx | 27 +++++++++++ hGameTest/src/assets/schema/HTexSchema.hx | 20 ++------ hGameTest/src/assets/tilesets/Sequence.hx | 1 - .../TextureAtlas.hx} | 17 ++++--- hGameTest/src/assets/tilesets/Tileset.hx | 13 ++++++ .../src/assets/tilesets/TilesetGeneric.hx | 16 ++++++- 12 files changed, 130 insertions(+), 132 deletions(-) create mode 100644 hGameTest/res/textures/sprites/missing.png delete mode 100644 hGameTest/src/assets/TextureData.hx create mode 100644 hGameTest/src/assets/files/TextureFile.hx rename hGameTest/src/assets/{SpriteSheet.hx => tilesets/TextureAtlas.hx} (63%) create mode 100644 hGameTest/src/assets/tilesets/Tileset.hx diff --git a/hGameTest/res/textures/sheets/sheet.htex.json b/hGameTest/res/textures/sheets/sheet.htex.json index 30ee1b76..1c427651 100644 --- a/hGameTest/res/textures/sheets/sheet.htex.json +++ b/hGameTest/res/textures/sheets/sheet.htex.json @@ -9,28 +9,28 @@ "tileSize":{ "x":16, "y":16 - }, - "tilesetGeneric":{ - "baseTexture":"textures/sheets/sheet.png", - "tiles":[ - "testTile1", - "testTile2", - "testTile3", - "testTile4", - "testTile5", - "testTile6", - "testTile7", - "testTile8", - "testTile9", - "testTile10", - "testTile11", - "testTile12", - "testTile13", - "testTile14", - "testTile15", - "testTile16" - ] } + }, + "tilesetGeneric":{ + "baseTexture":"textures/sheets/sheet.png", + "tiles":[ + "testTile1", + "testTile2", + "testTile3", + "testTile4", + "testTile5", + "testTile6", + "testTile7", + "testTile8", + "testTile9", + "testTile10", + "testTile11", + "testTile12", + "testTile13", + "testTile14", + "testTile15", + "testTile16" + ] } } } \ No newline at end of file diff --git a/hGameTest/res/textures/sheets/sheetAnim.htex.json b/hGameTest/res/textures/sheets/sheetAnim.htex.json index 04d58c08..cb10303f 100644 --- a/hGameTest/res/textures/sheets/sheetAnim.htex.json +++ b/hGameTest/res/textures/sheets/sheetAnim.htex.json @@ -2,35 +2,21 @@ "datascheme":"htex", "version":"1", "textureData":{ - "texName":"testsheet", + "texName":"testanimsheet", "texType":"atlas", "atlas":{ - "atlasType":"tilesetgeneric", + "atlasType":"tilesetanimated", "tileSize":{ "x":16, "y":16 }, - "tilesetGeneric":{ - "baseTexture":"textures/sheets/sheet.png", - "tiles":[ - "testTile1", - "testTile2", - "testTile3", - "testTile4", - "testTile5", - "testTile6", - "testTile7", - "testTile8", - "testTile9", - "testTile10", - "testTile11", - "testTile12", - "testTile13", - "testTile14", - "testTile15", - "testTile16" - ] - } + "tilesetAnimated":{ + "baseTexture":"textures/sheets/sheet.png", + "sequences":[ + {"name":"testsequence1","sequenceStart":0,"sequenceEnd":7}, + {"name":"testsequence2","sequenceStart":8,"sequenceEnd":15} + ] + } } } } \ No newline at end of file diff --git a/hGameTest/res/textures/sprites/missing.png b/hGameTest/res/textures/sprites/missing.png new file mode 100644 index 0000000000000000000000000000000000000000..d3f3d0c6d10e1cc2b0d9336b5960e09ebf9e4fbd GIT binary patch literal 161 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`EX7WqAsj$Z!;#VfC6!`MW(JQE4l}$x z{J@^E(yhdF$uj1qM28cpDbftw0k@VjJAGoFKSk4wfk9%v$lYxWjB9{)FnGH9xvX = []; public var bitmaps:Array; public var name:String = ""; public function new(_name:String){ - name = _name; - if(hTexArray[name] == null) - hTexArray[name] = name; + // name = _name; + // if(hTexArray[name] == null) + // hTexArray[name] = name; } public static function createTextureObjectFromJSON(path:String){ var DATA:TexV1; var loadedData:String = Assets.getText(path); var jsondata = Json.parse(loadedData); DATA = jsondata; - DATA = TexV1; - if(DATA.dataScheme == "htex"){ + if(DATA.datascheme == "htex"){ if(DATA.version == 1){ - var texdata:TextureDataV1 = Data.textureData; + var td:TextureDataV1 = DATA.textureData; + var width:Int = td.atlas.tileSize.x; + var height:Int = td.atlas.tileSize.y; + if(td.atlas.atlasType == "tilesetgeneric"){ + var baseTexturePath:String = td.tilesetGeneric.baseTexture; + var tileNames:Array = td.tilesetGeneric.tiles; + var atlas:TextureAtlas = new TextureAtlas(Assets.getBitmapData(baseTexturePath),width,height); + var tileSetGeneric = new TilesetGeneric(td.texName, atlas, tileNames); + trace(tileSetGeneric); + } } } } diff --git a/hGameTest/src/assets/TextureData.hx b/hGameTest/src/assets/TextureData.hx deleted file mode 100644 index 227ad5bb..00000000 --- a/hGameTest/src/assets/TextureData.hx +++ /dev/null @@ -1,44 +0,0 @@ -package assets; - -import openfl.Assets; -import haxe.Json; - -typedef TexturesV1={ - var dataScheme:String; - var version:Int; - var textureData:{ - var spriteSheets:Array<{ - var sheetNamekeke:String; - var path:String; - var sheetDataPath:String; - var explicitDimensions:Bool; - var tileDimensions:{ - var width:Int; - var height:Int; - }; - }>; - var sprites:Array<{ - var spriteName:String; - var path:String; - }>; - }; -} - -class TextureData{ - public static var textures:Array = []; - public static function parseConfig(path:String = "data/textures.json"){ - var DATA:TexturesV1; - var loadedData:String = Assets.getText(path); - var jsondata = Json.parse(loadedData); - DATA = jsondata; - //var test:String = File.getContent(test.txt); - trace(DATA); - /*typedef DataModal= - { - var ID:Int; - var NAME:String; - var TAGS:Array; - } - */ - } -} diff --git a/hGameTest/src/assets/files/TextureFile.hx b/hGameTest/src/assets/files/TextureFile.hx new file mode 100644 index 00000000..8f57e11a --- /dev/null +++ b/hGameTest/src/assets/files/TextureFile.hx @@ -0,0 +1,27 @@ +package assets; + +import openfl.display.BitmapData; +import openfl.Assets; +import haxe.Json; +import assets.tilesets.Tileset; + +class TextureFile{ + public static var pathMap:Map; + public static function addUser(path:String, user:Dynamic):Void + { + if(pathMap[path] == null){ + pathMap[path] = new TextureFile(); + } + pathMap[path].users.push(user); + } + public static function removeUser():Void + { + if() + } + public var bitmapData:BitmapData = null; + public var users:Array=[]; + public var path:String; + private function new(){ + + } +} diff --git a/hGameTest/src/assets/schema/HTexSchema.hx b/hGameTest/src/assets/schema/HTexSchema.hx index 66f4f3e6..bafc1f5e 100644 --- a/hGameTest/src/assets/schema/HTexSchema.hx +++ b/hGameTest/src/assets/schema/HTexSchema.hx @@ -6,7 +6,6 @@ typedef TileSetGenericV1={ var baseTexture:String; var tiles:Array; } - typedef TileSetAnimatedV1={ var baseTexture:String; var animations:Array<{ @@ -23,25 +22,14 @@ typedef TextureDataV1={ var tileSize:{ var x:Int; var y:Int; - }, - var tileSetGeneric:TileSetGenericV1; - var tileSetAnimated:TileSetAnimatedV1; + }; } - var path:String; - var sheetDataPath:String; - var explicitDimensions:Bool; - var tileDimensions:{ - var width:Int; - var height:Int; - }; - var sprites:Array<{ - var spriteName:String; - var path:String; - }>; + var tilesetGeneric:TileSetGenericV1; + var tilesetAnimated:TileSetAnimatedV1; } typedef TexV1={ - var dataScheme:String; + var datascheme:String; var version:Int; var textureData:TextureDataV1; } \ No newline at end of file diff --git a/hGameTest/src/assets/tilesets/Sequence.hx b/hGameTest/src/assets/tilesets/Sequence.hx index 1f63ad69..e843930b 100644 --- a/hGameTest/src/assets/tilesets/Sequence.hx +++ b/hGameTest/src/assets/tilesets/Sequence.hx @@ -4,7 +4,6 @@ import openfl.display.BitmapData; class Sequence{ public var bitmaps:Array = []; public var name:String = ""; - public var currentFrame = 0; public function new(_name:String, _bitmaps:Array){ name = _name; bitmaps = _bitmaps; diff --git a/hGameTest/src/assets/SpriteSheet.hx b/hGameTest/src/assets/tilesets/TextureAtlas.hx similarity index 63% rename from hGameTest/src/assets/SpriteSheet.hx rename to hGameTest/src/assets/tilesets/TextureAtlas.hx index 8573f842..ce278070 100644 --- a/hGameTest/src/assets/SpriteSheet.hx +++ b/hGameTest/src/assets/tilesets/TextureAtlas.hx @@ -1,25 +1,24 @@ -package assets; +package assets.tilesets; import openfl.geom.Point; import openfl.geom.Rectangle; import openfl.display.BitmapData; import openfl.display.Bitmap; -class SpriteSheet{ - public static var spriteSheetArray:Array = []; +class TextureAtlas{ + public static var tileSetArray:Array = []; public var sprites:Array = []; + public var columns:Int; + public var rows:Int; public function new(bmp:BitmapData = null, sprWidth:Int = 16, sprHeight:Int = 16){ - spriteSheetArray.push(this); - var columns:Int = cast((bmp.width / sprWidth),Int); - var rows:Int = cast((bmp.height / sprHeight),Int); - //trace("columns:"+columns); - //trace("rows"+rows); + tileSetArray.push(this); + columns = cast((bmp.width / sprWidth),Int); + rows = cast((bmp.height / sprHeight),Int); for(i in 0...rows){ for (j in 0...columns){ var bmpd:BitmapData = new BitmapData(sprWidth, sprHeight,true,0x00000000); var rect:Rectangle = new Rectangle(j*sprWidth,i*sprHeight,sprWidth,sprHeight); bmpd.copyPixels(bmp, rect, new Point(0,0)); sprites.push(bmpd); - //trace("i:"+i+",j:"+j); } } } diff --git a/hGameTest/src/assets/tilesets/Tileset.hx b/hGameTest/src/assets/tilesets/Tileset.hx new file mode 100644 index 00000000..5bc50acf --- /dev/null +++ b/hGameTest/src/assets/tilesets/Tileset.hx @@ -0,0 +1,13 @@ +package assets.tilesets; + + +class Tileset{ + public static var tilesetMap:Map = []; + public var texName:String; + public var texAtlas:TextureAtlas; + public function new(name:String, atlas:TextureAtlas){ + texName = name; + texAtlas = atlas; + tilesetMap[name] = this; + } +} \ No newline at end of file diff --git a/hGameTest/src/assets/tilesets/TilesetGeneric.hx b/hGameTest/src/assets/tilesets/TilesetGeneric.hx index 15423347..8456c8f8 100644 --- a/hGameTest/src/assets/tilesets/TilesetGeneric.hx +++ b/hGameTest/src/assets/tilesets/TilesetGeneric.hx @@ -1,8 +1,20 @@ package assets.tilesets; +import openfl.display.BitmapData; +import assets.tilesets.TextureAtlas; -class TilesetGeneric { - +class TilesetGeneric extends Tileset{ + public var tileMap:Map = []; + public function new(name:String, atlas:TextureAtlas ,tileNames:Array){ + super(name,atlas); + for(i in 0...texAtlas.sprites.length-1){ + trace(i); + trace(tileNames[i]); + var sprites:Array = texAtlas.sprites; + tileMap[tileNames[i]] = texAtlas.sprites[i]; + } + trace(tileMap); + } } \ No newline at end of file