Created
April 1, 2022 02:42
-
-
Save kaishuu0123/e4afff0a09c0e7657a6106aa818da4a7 to your computer and use it in GitHub Desktop.
AllenDang/giu で CHIP-8 エミュレータを書こうとした残骸
This file contains hidden or 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
package main | |
import ( | |
"fmt" | |
"image" | |
"image/color" | |
"github.com/AllenDang/giu" | |
g "github.com/AllenDang/giu" | |
"github.com/AllenDang/imgui-go" | |
) | |
var ( | |
multiline string | |
rgba *image.RGBA | |
tex *g.Texture | |
titleBarHeight float32 | |
windowFlags giu.WindowFlags = imgui.WindowFlagsNoCollapse | | |
imgui.WindowFlagsNoMove | | |
imgui.WindowFlagsNoResize | |
) | |
const ( | |
MASTER_WINDOW_WIDTH = 800 | |
MASTER_WINDOW_HEIGHT = 600 | |
DISPLAY_WIDTH = 640 | |
DISPLAY_HEIGHT = 320 | |
) | |
func buildMessage() g.Layout { | |
labels := make([]*g.LabelWidget, 10) | |
g.Label("CHIP-8 Dear ImGUI initialized") | |
g.Label("Please drag and drop CHIP-8's ROM") | |
for i := 0; i < 10; i++ { | |
message := fmt.Sprintf("Message %d", i) | |
labels[i] = g.Label(message) | |
} | |
return g.Layout([]g.Widget{labels[0], labels[1]}) | |
} | |
func loop() { | |
// refs: https://github.com/ocornut/imgui/issues/1539 | |
// titleBarHeight = imgui.FontSize() + (imgui.CurrentStyle().FramePadding().Y * 2) | |
g.NewTextureFromRgba(rgba, func(t *g.Texture) { | |
tex = t | |
}) | |
display := g.Window("Display") | |
logging := g.Window("Logging") | |
debug := g.Window("Debug") | |
// PushStyleVar したら PopStyleVar を呼び忘れないこと | |
imgui.PushStyleVarFloat(imgui.StyleVarWindowRounding, 0.0) | |
imgui.PushStyleVarVec2(imgui.StyleVarWindowPadding, imgui.Vec2{X: 0, Y: 0}) | |
display. | |
Flags(windowFlags). | |
Layout( | |
g.ImageWithRgba(rgba).OnClick(func() { | |
fmt.Println("rgba image was clicked") | |
}).Size(DISPLAY_WIDTH, DISPLAY_HEIGHT), | |
// g.Custom(func() { | |
// width, height := g.GetAvailableRegion() | |
// pos := g.GetCursorPos() | |
// canvas := g.GetCanvas() | |
// canvas.AddRectFilled(pos, pos.Add(image.Pt(int(width), int(height))), color.RGBA{12, 12, 200, 255}, 0, g.DrawFlagsClosed) | |
// }), | |
) | |
// Pop StyleVarWindowPadding | |
imgui.PopStyleVar() | |
displayW, displayH := display.CurrentSize() | |
widgetHeight := float32((MASTER_WINDOW_HEIGHT - DISPLAY_HEIGHT) / 2.0) | |
logging. | |
Flags(windowFlags). | |
Pos(0, displayH). | |
Size(displayW, widgetHeight). | |
Layout( | |
buildMessage(), | |
) | |
io := imgui.CurrentIO() | |
fpsMessage := fmt.Sprintf("Application average %.3f ms/frame (%.1f FPS)", 1000.0/.Framerate(), imgui.CurrentIO().Framerate()) | |
debug. | |
Flags(windowFlags). | |
Pos(0, displayH+widgetHeight). | |
Layout( | |
) | |
// Pop StyleVarWindowRounding | |
imgui.PopStyleVar() | |
} | |
func grad(img *image.RGBA) { | |
rect := img.Rect | |
for h := rect.Min.Y; h < rect.Max.Y; h++ { | |
for v := rect.Min.X; v < rect.Max.X; v++ { | |
img.Set(v, h, color.RGBA{ | |
uint8(255 * h / rect.Max.Y), | |
uint8(255 * v / rect.Max.X), | |
uint8(255 * v * h / (rect.Max.X * rect.Max.Y)), | |
255}) | |
} | |
} | |
} | |
func main() { | |
rgba = image.NewRGBA(image.Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT)) | |
grad(rgba) | |
w := g.NewMasterWindow("Chip-8 Dear ImGUI", MASTER_WINDOW_WIDTH, MASTER_WINDOW_HEIGHT, g.MasterWindowFlagsNotResizable) | |
w.SetBgColor(color.RGBA{255, 255, 255, 255}) | |
// g.EnqueueNewTextureFromRgba(rgba, func(t *g.Texture) { | |
// tex = t | |
// }) | |
w.Run(loop) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment