Untitled

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
3.1 kB
3
Indexable
void UIProc::update(delta_type Delta)
	{
		if (!getGlobal<UIBuffer>(GlobTypes::UIBuffer)) 
			return nk_clear(&nuklear_ctx.ctx);

		static auto *inputState = getGlobal<InputState>(GlobTypes::InputState);
		static auto *TripleBuff = getGlobal<TripleBuffer<UIBuffer>>(GlobTypes::UIBuffer);
		
		// window minimized
		if (inputState->window_width == 0 || inputState->window_height == 0)
			return nk_clear(&nuklear_ctx.ctx);

		// pump input before starting process
		pumpInput();

		// any widget is hover?
		inputState->anyUIHovered = isAnyUIHovered();

		// generate and upload vertex and index data
		//nk_buffer_clear(&nuklear_ctx.nkvbuf);
		//nk_buffer_clear(&nuklear_ctx.nkibuf);
		//nk_buffer_clear(&nuklear_ctx.nkcmds);
		//nk_convert(&nuklear_ctx.ctx, &nuklear_ctx.nkcmds, &nuklear_ctx.nkvbuf, &nuklear_ctx.nkibuf, &nuklear_ctx.config);

		// any change in UI since last frame? if no, we can skip this frame.
		auto *cmdMem = static_cast<u8 *>(nk_buffer_memory(&nuklear_ctx.ctx.memory));
		if (memcmp(cmdMem, nuklear_ctx.lastCbuf, nuklear_ctx.ctx.memory.allocated) == 0)
			return nk_clear(&nuklear_ctx.ctx);

		// store last cmmands buffer for compare at next frame
		memcpy(nuklear_ctx.lastCbuf, cmdMem, nuklear_ctx.ctx.memory.allocated);

		// get current gfx writing buffer
		auto renderBuffer = TripleBuff->getWriting();
		// reset counts
		renderBuffer->indCount = 0;
		renderBuffer->vertCount = 0;
		renderBuffer->drawCount = 0;

		// generate and upload vertex and index data
		nk_buffer_clear(&nuklear_ctx.nkvbuf);
		nk_buffer_clear(&nuklear_ctx.nkibuf);
		nk_buffer_clear(&nuklear_ctx.nkcmds);
		nk_convert(&nuklear_ctx.ctx, &nuklear_ctx.nkcmds, &nuklear_ctx.nkvbuf, &nuklear_ctx.nkibuf, &nuklear_ctx.config);

		// copy verts
		renderBuffer->vertCount = nuklear_ctx.nkvbuf.needed / sizeof(UIVertex);
		if (nuklear_ctx.nkvbuf.needed > 0) 
			memcpy(renderBuffer->verts, nuklear_ctx.vbuf, nuklear_ctx.nkvbuf.needed);

		// copy indexes
		renderBuffer->indCount = nuklear_ctx.nkibuf.needed / sizeof(u16);
		if (nuklear_ctx.nkibuf.needed > 0) 
			memcpy(renderBuffer->index, nuklear_ctx.ibuf, nuklear_ctx.nkibuf.needed);

		// render draw commands
		const struct nk_draw_command* cmd = nullptr;
		u32 ioffset = 0;
		u32 coffset = 0;
		nk_draw_foreach(cmd, &nuklear_ctx.ctx, &nuklear_ctx.nkcmds)
		{
			// draw count
			renderBuffer->drawOffsets[coffset] = cmd->elem_count;
			// scissor
			// note: we have to add a extra +1 to scissors boundaries because of vulkan
			renderBuffer->scissors[coffset] = recti(std::max(cmd->clip_rect.x - 1, 0.0f), std::max(cmd->clip_rect.y - 1, 0.0f),
			                                        cmd->clip_rect.x < 0.0f ? cmd->clip_rect.w + cmd->clip_rect.x + 1 : cmd->clip_rect.w + 1,
													cmd->clip_rect.y < 0.0f ? cmd->clip_rect.h + cmd->clip_rect.y + 1 : cmd->clip_rect.h + 1);
			// increase offsets
			ioffset += cmd->elem_count;
			++coffset;
			++renderBuffer->drawCount;
		}

		// release buffer for read on another threads
		TripleBuff->flipWriting();

		// cleanup
		nk_clear(&nuklear_ctx.ctx);
	}