Example #1
0
func CreateResourceManager(config *ResourceManagerConfig) *ResourceManager {
	/*	defaultTileConfig := TileConfig{Name: DEFAULT_TILE_NAME, Filename: DEFAULT_TILE_NAME}
		config.TileConfigs = append(config.TileConfigs, defaultTileConfig)

		tileConfs := make([]TileConfig, 0)
		for _, v := range config.TileConfigs {
			tileConfs = append(tileConfs, v)
		}

		var manager ResourceManager

		nBmps := len(tileConfs)
		log.Printf("Loading %v bitmaps", nBmps)

		loadedBmps := make([]*allegro.Bitmap, nBmps)
		manager.tileMetadatas = make([]tileMetadata, nBmps)
		var wg sync.WaitGroup
		wg.Add(nBmps)
		for j := 0; j < nBmps; j++ {
			go func (i int) {
				cfg := tileConfs[i]

				// Load the bitmap
				var bmp *allegro.Bitmap
				if cfg.Name == DEFAULT_TILE_NAME {
					bmp = allegro.NewBitmap(DEFAULT_TILE_WIDTH, DEFAULT_TILE_HEIGHT)
				} else {
					bmp = allegro.LoadBitmap(cfg.Filename)
				}
				if bmp == nil {
					log.Fatalf("Failed to load bmp %v", cfg)
				}

				loadedBmps[i] = bmp
				manager.tileMetadatas[i] = generateMetadata(bmp, cfg)

				wg.Done()
			} (j)
		}
		wg.Wait()
		log.Printf("Loaded bitmaps")

		manager.tileBmps = make([]*Bitmap, nBmps)
		for i, bmp := range loadedBmps {
			metadata := manager.tileMetadatas[i]
			fBmp := &Bitmap{gl.Texture(bmp.GetGLTexture()), bmp,
				metadata.offx, metadata.offy, metadata.w, metadata.h}
			manager.tileBmps[i] = fBmp
		}

		manager.tilePos = make(map[string]int, nBmps)
		for i, metadata := range manager.tileMetadatas {
			manager.tilePos[metadata.name] = i
		}*/

	var manager ResourceManager
	manager.tileMetadatas = make(map[string]tileMetadata)
	manager.tileBmps = make(map[string]Bitmap)

	// Load the fonts
	manager.fontMap = make(map[string]*allegro.Font)
	for _, v := range config.FontConfigs {
		var font *allegro.Font
		if v.Filename == "builtin" {
			font = allegro.CreateBuiltinFont()
		} else {
			font = allegro.LoadFont(v.Filename, v.Size, 0)
		}
		manager.fontMap[v.Name] = font
	}

	return &manager
}
Example #2
0
func (d *DisplayEngine) drawFrame() {
	toDraw := make([][]*resources.Bitmap, d.config.MapW*d.config.MapH)
	drawPasses := 0
	for x := 0; x < d.config.MapW; x++ {
		for y := 0; y < d.config.MapH; y++ {
			toDraw[x*d.config.MapW+y] = (*d.gameEngine).GetTile(x, y)
			length := len(toDraw[x*d.config.MapW+y])
			if length > drawPasses {
				drawPasses = length
			}
		}
	}

	viewport := d.viewport
	font := allegro.CreateBuiltinFont()

	// Don't want anyone changing the viewport mid frame or any such highjinks
	d.Display.SetTargetBackbuffer()

	allegro.RunInThread(func() {
		r, g, b, a := d.config.BGColor.GetRGBA()
		gl.ClearColor(
			gl.GLclampf(r)/255.0,
			gl.GLclampf(g)/255.0,
			gl.GLclampf(b)/255.0,
			gl.GLclampf(a)/255.0)

		gl.Clear(gl.COLOR_BUFFER_BIT)

		viewport.SetupTransform()

		for p := 0; p < drawPasses; p++ {
			m := d.config.MapW
			n := d.config.MapH
			for s := 0; s < m+n; s++ {
				for x := 0; x < s; x++ {
					y := s - x - 1
					if x >= m || y < 0 || y >= n {
						continue
					}
					if len(toDraw[x*d.config.MapW+y]) < p {
						continue
					}

					// Coordinates in terms of pixels
					px := (y - x) * d.config.TileW / 2
					py := (x + y) * d.config.TileH / 2
					bmp := toDraw[x*d.config.MapW+y][p]
					/*					ox := bmp.OffX
										oy := bmp.OffY*/
					bw, bh := bmp.W, bmp.H
					if viewport.OnScreen(px, py, bw, bh) {
						gl.Begin(gl.QUADS)
						bmp.Tex.Bind(gl.TEXTURE_2D)
						gl.TexCoord2f(0, 0)
						gl.Vertex3i(px, py, 0)
						gl.TexCoord2f(0, 1)
						gl.Vertex3i(px, py+bw, 0)
						gl.TexCoord2f(1, 1)
						gl.Vertex3i(px+bh, py+bw, 0)
						gl.TexCoord2f(1, 0)
						gl.Vertex3i(px+bh, py, 0)
						gl.End()
					}
				}
			}
		}

		gl.Flush()
	})

	var trans allegro.Transform
	trans.Identity()
	trans.Use()

	font.Draw(allegro.CreateColor(0, 255, 0, 255), 0, 0, 0, fmt.Sprint(int(d.fps)))

	allegro.Flip()

	d.frameDrawing.Unlock()
}