Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

drawing 185 lines or more freezes up under certain conditions #73

Open
Moth-Tolias opened this issue Dec 6, 2024 · 0 comments
Open
Assignees
Labels
help wanted Extra attention is needed

Comments

@Moth-Tolias
Copy link
Contributor

hello. i'm having an extremely strange issue. the following code freezes up when drawing 185 lines or more; it doesn't crash, it just stops updating or responding to input, and i have to kill it from the console with ctrl + c.

import std.stdio;
import bindbc.sdl;

void main()
{
	SDL_Window* window;
	SDL_Renderer* renderer;
	bool isRunning = true;

	loadSDL();
	SDL_Init(SDL_INIT_VIDEO);

	enum windowSizeX = 512;
	enum windowSizeY = 288;
	enum windowTitle = "line test";

	window = createWindow(windowSizeX, windowSizeY, windowTitle);
	renderer = createRenderer(window);

	int lines = 0;

	while(isRunning)
	{
		SDL_Event currentSDLEvent;
		while (SDL_PollEvent(&currentSDLEvent) != 0)
		{
			if (currentSDLEvent.type == SDL_QUIT)
			{
				isRunning = false;

				import core.stdc.stdio: printf;
				printf("quitting...\n");
			}
		}

		SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF);
		SDL_RenderClear(renderer); //required

		SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
		drawLines(renderer, lines);

		SDL_RenderPresent(renderer); //required
	}
}

SDL_Window* createWindow(int windowSizeX, int windowSizeY, string windowTitle)
{
	import std.string: toStringz;
	enum windowFlags = SDL_WINDOW_SHOWN;
	SDL_Window* result = SDL_CreateWindow(
		toStringz(windowTitle),
		SDL_WINDOWPOS_UNDEFINED, // initial x position
		SDL_WINDOWPOS_UNDEFINED, // initial y position
		windowSizeX,
		windowSizeY,
		windowFlags
	);

	return result;
}

SDL_Renderer* createRenderer(SDL_Window* window)
{
	enum rendererFlags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC;
	enum SDL_firstAvailableDriver = -1;
	SDL_Renderer* result = SDL_CreateRenderer(window, SDL_firstAvailableDriver, rendererFlags);
	return result;
}


void drawLines(SDL_Renderer* renderer, ref int noOfLines)
{
	foreach(line; 0 .. noOfLines += 1) // += is required?? ++ seemingly works fine
	{
		immutable int x = (line % 100) * 3;
		immutable int y = (line / 100) * 3;

		import std.math;
		immutable wtf = cast(int) (sin(cast(float)line) * 4); // different multipliers have different results, eg:
		// 1 seemingly has no limit
		// 2 halts at 449
		// 3 halts at 371
		// 4, 5, 6, 7 halt at 185
		// 8, 9 halts at 356
		// 10 halts at 497

		SDL_RenderDrawLine(renderer, x, y, x + 1 + wtf, y + 1); // the + 1s are required???
	}

	import std.stdio: writeln;
	writeln(noOfLines);
}

i haven't the foggiest idea why this would happen, least of all why += should be different from ++ - nor why the +1s seem to be required.

using version 1.5.0, dmd, windows 10.

@ichordev ichordev added the help wanted Extra attention is needed label Dec 8, 2024
@ichordev ichordev self-assigned this Dec 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants