Exemplo n.º 1
0
    math::vector<int, 2> window::get_mouse_position() const
    {
        int x, y;
        SDL_GetGlobalMouseState(&x, &y);

        int window_x, window_y;
        SDL_GetWindowPosition(handle.get(), &window_x, &window_y);

        x -= window_x;
        y -= window_y;

        // SDL_GetGlobalMouseState and SDL_GetWindowPosition return "screen
        // coordinates", as in scaled values. We'll need to convert these to
        // pixels before we can do anything with them.

        // TODO: is this also the case on Windows?

        // TODO: seeing as get_position/set_position and get_size/set_size
        // operate in "screen coordinates", there might also be use for a
        // variant of get_mouse_position that returns "screen coordinates".

        x *= get_backbuffer_width() / get_width();
        y *= get_backbuffer_height() / get_height();

        return { x, y };
    }
Exemplo n.º 2
0
	void InputManager::Update()
	{
		// Mouse motion only since we cannot rely on MouseMotion events from SDL because we might reset the mouse.
		{
			int x = m_OldMousePositionX, y = m_OldMousePositionY;
			SDL_GetGlobalMouseState(&x, &y);

			if (x != m_OldMousePositionX || y != m_OldMousePositionY)
			{
				MouseInputEvent mouseEvent = MouseInputEvent(MOUSE_MOTION_ONLY, NO_CLICK, MOUSE_NO_MODIFIER, x, y, x - m_OldMousePositionX, y - m_OldMousePositionY);
				NotifyMouseListener(mouseEvent);
			}

			if (m_ResetMouseToPosition)
			{
				ResetMouseToCenter();
			}
			else
			{
				m_OldMousePositionX = x;
				m_OldMousePositionY = y;
			}


		}

	}
Exemplo n.º 3
0
FVector2D FLinuxCursor::GetPosition() const
{
	int CursorX, CursorY;

	SDL_GetGlobalMouseState(&CursorX, &CursorY);
	CursorX += AccumulatedOffsetX;
	CursorY += AccumulatedOffsetY;

	return FVector2D( CursorX, CursorY );
}
Exemplo n.º 4
0
static void ImGui_ImplSDL2_UpdateMousePosAndButtons()
{
    ImGuiIO& io = ImGui::GetIO();
    const ImVec2 mouse_pos_backup = io.MousePos;
    io.MousePos = ImVec2(-FLT_MAX, -FLT_MAX);

    // Set OS mouse position if requested (rarely used, only when ImGuiConfigFlags_NavEnableSetMousePos is enabled by user)
    // (When multi-viewports are enabled, all imgui positions are same as OS positions.)
#if SDL_HAS_WARP_MOUSE_GLOBAL
    if (io.WantSetMousePos)
        SDL_WarpMouseGlobal((int)mouse_pos_backup.x, (int)mouse_pos_backup.y);
#endif

    int mx, my;
    Uint32 mouse_buttons = SDL_GetMouseState(&mx, &my);
    io.MouseDown[0] = g_MousePressed[0] || (mouse_buttons & SDL_BUTTON(SDL_BUTTON_LEFT)) != 0;  // If a mouse press event came, always pass it as "mouse held this frame", so we don't miss click-release events that are shorter than 1 frame.
    io.MouseDown[1] = g_MousePressed[1] || (mouse_buttons & SDL_BUTTON(SDL_BUTTON_RIGHT)) != 0;
    io.MouseDown[2] = g_MousePressed[2] || (mouse_buttons & SDL_BUTTON(SDL_BUTTON_MIDDLE)) != 0;
    g_MousePressed[0] = g_MousePressed[1] = g_MousePressed[2] = false;

#if SDL_HAS_CAPTURE_MOUSE
    SDL_Window* focused_window = SDL_GetKeyboardFocus();
    if (g_Window == focused_window)
    {
        // SDL_GetMouseState() gives mouse position seemingly based on the last window entered/focused(?)
        // The creation of a new windows at runtime and SDL_CaptureMouse both seems to severely mess up with that, so we retrieve that position globally.
        int wx, wy;
        SDL_GetWindowPosition(focused_window, &wx, &wy);
        SDL_GetGlobalMouseState(&mx, &my);
        mx -= wx;
        my -= wy;
        io.MousePos = ImVec2((float)mx, (float)my);
    }

    // SDL_CaptureMouse() let the OS know e.g. that our imgui drag outside the SDL window boundaries shouldn't e.g. trigger the OS window resize cursor. 
    // The function is only supported from SDL 2.0.4 (released Jan 2016)
    bool any_mouse_button_down = ImGui::IsAnyMouseDown();
    SDL_CaptureMouse(any_mouse_button_down ? SDL_TRUE : SDL_FALSE);
#else
    if (SDL_GetWindowFlags(g_Window) & SDL_WINDOW_INPUT_FOCUS)
        io.MousePos = ImVec2((float)mx, (float)my);
#endif
}
Exemplo n.º 5
0
int main(int argc, char **argv)
{
	char nom_du_fichier[300] = "No_File_Loaded";
	if (argc > 1)
	{
		strcpy_s(nom_du_fichier, argv[1]);
	}
	char temp[20] = "";
	SDL_Window *window = nullptr;
	SDL_Renderer *render = nullptr;
	Input INPUT; inputInit(&INPUT);
	SDL_Color WHITE = { 255, 255 , 255 };
	int volume = 70;
	initVid(&window, &render, w_window, h_window);
	SDL_SetWindowTitle(window, "ColdJuke");

	TTF_Init();
	Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, MIX_DEFAULT_CHANNELS, 1024);

	Mix_Music *music = nullptr;
	music = Mix_LoadMUS(nom_du_fichier);
	Mix_PlayMusic(music, -1);
	Mix_VolumeMusic(static_cast<double>(128 / 100)*volume);
	clearWithColor(render, 80, 40, 100, 255);
	SDL_Texture *gui = imgToTex((baseProgramPath + "gui.png").c_str(), render);
	
	sprintf_s(temp, "%d%%", volume);
	SDL_Texture *tex_volume = txtToTex(temp, render, WHITE, 14);
	SDL_Texture *texte = txtToTex(nom_du_fichier, render, WHITE, 14);
	afficherGui(gui, texte, tex_volume, render);

	SDL_RenderPresent(render);
	int FPS(60), TICKS(1000 / FPS);
	long start(SDL_GetTicks()), actual(SDL_GetTicks());
	INPUT.filedrop = "";
	while (!INPUT.quit && !INPUT.key[SDL_SCANCODE_ESCAPE] && !(INPUT.leftclic && INPUT.leftclic_position_x > w_window-32 && INPUT.leftclic_position_y < 32))
	{
		inputReturn(&INPUT);

		actual = SDL_GetTicks();
		if (actual - start > TICKS)
		{
			if (INPUT.key[SDL_SCANCODE_SPACE] || (INPUT.leftclic && INPUT.leftclic_position_x < 64 && INPUT.leftclic_position_y > 64)) // Pause Play
			{
				if (Mix_PausedMusic())
				{
					Mix_ResumeMusic();
					texToRenderPart(gui, render, 100, 0, 64, 64, 64, 32, 0, 64, 64);
					SDL_RenderPresent(render);

				}
				else
				{
					Mix_PauseMusic();
					texToRenderPart(gui, render, 100, 0, 64, 64, 64, 32, 64, 64, 64);
					SDL_RenderPresent(render);

				}
				SDL_Delay(250);
			}
			if (INPUT.wheelup)
			{
				volume += 2;
				if (volume > 100)
					volume = 100;
				Mix_VolumeMusic(static_cast<double>(128 / 100)*volume);
				// Volume 
				texToRenderPart(gui, render, 100, w_window - 64, 64, 64, 64, 96, 0, 64, 64);
				sprintf_s(temp, "%d%%", volume);
				SDL_DestroyTexture(tex_volume);
				tex_volume = txtToTex(temp, render, WHITE, 14);
				// Volume Number
				texToRender(tex_volume, render, 100, w_window - 40, 85);
				SDL_RenderPresent(render);


			}
			if (INPUT.wheeldown)
			{
				volume -= 2;
				if (volume < 0)
					volume = 0;
				Mix_VolumeMusic(static_cast<double>(128 / 100)*volume);
				// Volume 
				texToRenderPart(gui, render, 100, w_window - 64, 64, 64, 64, 96, 0, 64, 64);
				sprintf_s(temp, "%d%%", volume);
				SDL_DestroyTexture(tex_volume);
				tex_volume = txtToTex(temp, render, WHITE, 14);
				// Volume Number
				texToRender(tex_volume, render, 100, w_window - 40, 85);
				SDL_RenderPresent(render);

			}
			if (INPUT.rightclic)
			{
				int x_mouse_screen; int y_mouse_screen;
				SDL_GetGlobalMouseState(&x_mouse_screen,&y_mouse_screen);
				SDL_SetWindowPosition(window, x_mouse_screen-INPUT.rightclic_position_x, y_mouse_screen-INPUT.rightclic_position_y);
				inputReturn(&INPUT);
			}
			if (INPUT.filedrop != "")
			{
				Mix_PauseMusic();
				music = Mix_LoadMUS(INPUT.filedrop);
				strcpy_s(nom_du_fichier, INPUT.filedrop);
				Mix_PlayMusic(music, -1);
				sprintf_s(temp, "%d%%", volume);
				SDL_DestroyTexture(texte);
				texte = txtToTex(nom_du_fichier, render, WHITE, 14);
				afficherGui(gui, texte, tex_volume, render);
				SDL_RenderPresent(render);

			}
			start = actual;
			INPUT.filedrop = "";
			INPUT.wheeldown = 0;
			INPUT.wheelup = 0;
		}
		else
		{
			SDL_Delay(TICKS - (actual - start));
		}
	}

	Mix_CloseAudio();
	TTF_Quit();
	SDL_Quit();
	return 0;
}