|
| 1 | +#include <stdio.h> |
| 2 | +#include <vector> |
| 3 | +#include <string> |
| 4 | +#include <lumino.h> |
| 5 | + |
| 6 | +static size_t GetFileSize(FILE* stream) { |
| 7 | + struct stat stbuf; |
| 8 | + int handle = fileno(stream); |
| 9 | + if (handle == 0) return 0; |
| 10 | + if (fstat(handle, &stbuf) == -1) return 0; |
| 11 | + return stbuf.st_size; |
| 12 | +} |
| 13 | + |
| 14 | +std::vector<uint8_t> ReadAllBytes(const std::string& filePath) { |
| 15 | + FILE* file = fopen(filePath.c_str(), "rb"); |
| 16 | + if (!file) { |
| 17 | + printf("Error: %s\n", filePath.c_str()); |
| 18 | + return {}; |
| 19 | + } |
| 20 | + size_t size = GetFileSize(file); |
| 21 | + std::vector<uint8_t> buffer(size); |
| 22 | + fread(buffer.data(), 1, size, file); |
| 23 | + fclose(file); |
| 24 | + return buffer; |
| 25 | +} |
| 26 | + |
| 27 | +int main() { |
| 28 | + LNConfig_SetGraphicsBackend(LN_GRAPHICS_BACKEND_WEBGPU); |
| 29 | + |
| 30 | + LNResult result = LNInstance_Initialize(); |
| 31 | + if (result != LN_OK) { |
| 32 | + return 1; |
| 33 | + } |
| 34 | + |
| 35 | + LNHandle window = LN_NULL_HANDLE; |
| 36 | + LNHandle graphicsContext = LN_NULL_HANDLE; |
| 37 | + LNHandle viewPoint = LN_NULL_HANDLE; |
| 38 | + LNWindow_Create(640, 480, "RenderItem Example", &window); |
| 39 | + LNWindow_GetGraphicsContext(window, &graphicsContext); |
| 40 | + LNViewPoint_Create(&viewPoint); |
| 41 | + |
| 42 | + // テクスチャの読み込み |
| 43 | + std::string assetsDir = ASSETS_DIR; |
| 44 | + std::vector<uint8_t> imageData = ReadAllBytes(assetsDir + "/icon256.png"); |
| 45 | + LNHandle texture1 = LN_NULL_HANDLE; |
| 46 | + if (LNTexture2D_CreateFromImageFileData(imageData.data(), imageData.size(), &texture1) != LN_OK) { |
| 47 | + return 1; |
| 48 | + } |
| 49 | + |
| 50 | + // マテリアルの作成 |
| 51 | + LNHandle material1 = LN_NULL_HANDLE; |
| 52 | + LNMaterial_Create(&material1); |
| 53 | + LNMaterial_SetMainTexture(material1, texture1); |
| 54 | + |
| 55 | + // RenderItem の作成と設定 (事前に作成しておく) |
| 56 | + LNHandle renderItem = LN_NULL_HANDLE; |
| 57 | + LNRenderItem_Create(&renderItem); |
| 58 | + |
| 59 | + // スプライトを追加 |
| 60 | + LNVector2 size = {100.0f, 100.0f}; |
| 61 | + LNVector2 anchorRatio = {0.5f, 0.5f}; |
| 62 | + LNRect uvRect = {0.0f, 0.0f, 1.0f, 1.0f}; |
| 63 | + LNRenderItem_AddSprite(renderItem, &size, texture1, &uvRect, &anchorRatio); |
| 64 | + |
| 65 | + // マテリアルと基本設定 |
| 66 | + LNRenderItem_SetMaterial(renderItem, material1); |
| 67 | + LNRenderItem_SetBillboardType(renderItem, LN_BILLBOARD_TYPE_NONE); |
| 68 | + LNRenderItem_SetBaseDirection(renderItem, LN_SPRITE_BASE_DIRECTION_BASIC2D); |
| 69 | + |
| 70 | + // 色を設定 |
| 71 | + LNColor white = {1.0f, 1.0f, 1.0f, 1.0f}; |
| 72 | + LNRenderItem_SetColor(renderItem, &white); |
| 73 | + |
| 74 | + // 2つ目の RenderItem (別の位置に表示) |
| 75 | + LNHandle renderItem2 = LN_NULL_HANDLE; |
| 76 | + LNRenderItem_Create(&renderItem2); |
| 77 | + LNRenderItem_AddSprite(renderItem2, &size, texture1, &uvRect, &anchorRatio); |
| 78 | + LNRenderItem_SetMaterial(renderItem2, material1); |
| 79 | + LNRenderItem_SetBillboardType(renderItem2, LN_BILLBOARD_TYPE_NONE); |
| 80 | + LNRenderItem_SetBaseDirection(renderItem2, LN_SPRITE_BASE_DIRECTION_BASIC2D); |
| 81 | + LNColor tint = {1.0f, 0.5f, 0.5f, 1.0f}; // 赤みがかった色 |
| 82 | + LNRenderItem_SetColor(renderItem2, &tint); |
| 83 | + |
| 84 | + int frameCount = 0; |
| 85 | + while (true) { |
| 86 | + LNBool quit = LN_FALSE; |
| 87 | + LNInstance_ProcessEvents(); |
| 88 | + LNInstance_ShouldQuit(&quit); |
| 89 | + if (quit) { |
| 90 | + break; |
| 91 | + } |
| 92 | + |
| 93 | + int width = 0; |
| 94 | + int height = 0; |
| 95 | + LNHandle colorBuffer = LN_NULL_HANDLE; |
| 96 | + LNHandle depthBuffer = LN_NULL_HANDLE; |
| 97 | + LNWindow_GetFramebufferSize(window, &width, &height); |
| 98 | + LNGraphicsContext_BeginFrame(graphicsContext, width, height, &colorBuffer, &depthBuffer); |
| 99 | + |
| 100 | + LNViewPoint_SetupOrtho2D(viewPoint, 0, 0, 0, width, height, -500, 500); |
| 101 | + |
| 102 | + LNHandle renderingPass = LN_NULL_HANDLE; |
| 103 | + LNRenderPassDescriptor descriptor; |
| 104 | + descriptor.renderTargets[0].renderTarget = colorBuffer; |
| 105 | + descriptor.renderTargets[0].clearColor[0] = 0.20f; |
| 106 | + descriptor.renderTargets[0].clearColor[1] = 0.25f; |
| 107 | + descriptor.renderTargets[0].clearColor[2] = 0.30f; |
| 108 | + descriptor.renderTargets[0].clearColor[3] = 1.0f; |
| 109 | + descriptor.renderTargets[0].clearEnable = LN_TRUE; |
| 110 | + descriptor.depthBuffer.depthBuffer = depthBuffer; |
| 111 | + descriptor.depthBuffer.clearDepth = 1.0f; |
| 112 | + descriptor.depthBuffer.clearStencil = 0; |
| 113 | + descriptor.depthBuffer.clearDepthEnable = LN_TRUE; |
| 114 | + descriptor.depthBuffer.clearStencilEnable = LN_TRUE; |
| 115 | + LNGraphicsContext_BeginSceneRenderPass(graphicsContext, descriptor, viewPoint, &renderingPass); |
| 116 | + |
| 117 | + // RenderItem の位置を更新 (毎フレーム円運動) |
| 118 | + float x1 = 320.0f + 100.0f * cosf(0.05f * frameCount); |
| 119 | + float y1 = 240.0f + 100.0f * sinf(0.05f * frameCount); |
| 120 | + LNRenderItem_SetPosition(renderItem, x1, y1, 0.0f); |
| 121 | + |
| 122 | + // 2つ目の RenderItem は逆回転 |
| 123 | + float x2 = 320.0f + 100.0f * cosf(-0.05f * frameCount + 3.14159f); |
| 124 | + float y2 = 240.0f + 100.0f * sinf(-0.05f * frameCount + 3.14159f); |
| 125 | + LNRenderItem_SetPosition(renderItem2, x2, y2, 0.0f); |
| 126 | + |
| 127 | + // 回転も設定 (Z軸回転) |
| 128 | + LNRenderItem_SetRotation(renderItem, 0.0f, 0.0f, frameCount * 2.0f); |
| 129 | + LNRenderItem_SetRotation(renderItem2, 0.0f, 0.0f, -frameCount * 2.0f); |
| 130 | + |
| 131 | + // RenderItem を描画 (実際の描画内容は事前に設定済み) |
| 132 | + LNSceneRenderPass_DrawRenderItem(renderingPass, renderItem); |
| 133 | + LNSceneRenderPass_DrawRenderItem(renderingPass, renderItem2); |
| 134 | + |
| 135 | + LNGraphicsContext_EndSceneRenderPass(graphicsContext, renderingPass); |
| 136 | + LNGraphicsContext_EndFrame(graphicsContext); |
| 137 | + LNWindow_Present(window); |
| 138 | + |
| 139 | + frameCount++; |
| 140 | + } |
| 141 | + |
| 142 | + // リソースの解放 |
| 143 | + LNObject_Release(renderItem); |
| 144 | + LNObject_Release(renderItem2); |
| 145 | + LNObject_Release(material1); |
| 146 | + LNObject_Release(texture1); |
| 147 | + LNObject_Release(viewPoint); |
| 148 | + LNObject_Release(window); |
| 149 | + LNInstance_Terminate(); |
| 150 | + return 0; |
| 151 | +} |
0 commit comments