Пример #1
0
int main(int argc, char *argv[])
{
	(void) argc;
	(void) argv;
	SDL_CHECK(SDL_Init(SDL_INIT_VIDEO) == 0);
	atexit(SDL_Quit);

	TTF_CHECK(TTF_Init() == 0);
	atexit(liberer_polices);

	window = SDL_SetVideoMode(W, H, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
	SDL_CHECK(window != NULL);

	state = creer_menu();

#ifdef EMSCRIPTEN
	emscripten_set_main_loop(update, 0, true);
#else
	while (!state->quitter) {
		update();
		SDL_Delay(1000 / 60.);
	}
#endif
	state->destructeur(state);

	return EXIT_SUCCESS;
}
Пример #2
0
void DEV_Joystick::Init()
{
#ifdef WITH_SDL

	if (!(SDL_CHECK(SDL_InitSubSystem)) || !(SDL_CHECK(SDL_GameControllerAddMapping))) {
		return;
	}

	/* Initializing Game Controller related subsystems */
	bool success = (SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER | SDL_INIT_HAPTIC) != -1 );

	if (success) {

		/* Loading Game Controller mapping data base from a string */
		unsigned short i = 0;
		const char *mapping_string = NULL;
		mapping_string = controller_mappings[i];

		while (mapping_string) {
			SDL_GameControllerAddMapping(mapping_string);
			i++;
			mapping_string = controller_mappings[i];
	    }
	}
	else {
		CM_Error("initializing SDL Game Controller: " << SDL_GetError());
	}
#endif
}
Пример #3
0
void skin_surface_update(SkinSurface* s, SkinRect* r) {
    if (!s || !s->texture) {
        return;
    }

    // With SDL2, always update the whole texture to the window, and
    // ignore the rectangle.
    (void)r;

    SDL_Renderer* renderer = globals_get_renderer();
    SDL_CHECK(SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255));
    SDL_CHECK(SDL_RenderClear(renderer));
    SDL_CHECK(SDL_RenderCopy(renderer, s->texture, NULL, NULL));
    SDL_RenderPresent(renderer);
}
Пример #4
0
const std::string DEV_Joystick::GetName()
{
#ifdef WITH_SDL
	return (SDL_CHECK(SDL_GameControllerName)) ? SDL_GameControllerName(m_private->m_gamecontroller) : "";
#else /* WITH_SDL */
	return "";
#endif /* WITH_SDL */
}
Пример #5
0
void DEV_Joystick::DestroyJoystickDevice(void)
{
#ifdef WITH_SDL
	if (m_isinit) {

			if (m_private->m_haptic && SDL_CHECK(SDL_HapticClose)) {
				SDL_HapticClose(m_private->m_haptic);
				m_private->m_haptic = NULL;
			}

			if (m_private->m_gamecontroller && SDL_CHECK(SDL_GameControllerClose)) {
				CM_Debug("Game Controller (" << GetName() << ") with index " << m_joyindex << " closed");
				SDL_GameControllerClose(m_private->m_gamecontroller);
				m_private->m_gamecontroller = NULL;
			}

		m_isinit = false;
	}
#endif /* WITH_SDL */
}
Пример #6
0
bool DEV_Joystick::aButtonReleaseIsPositive(int button)
{
#ifdef WITH_SDL
	if (!(SDL_CHECK(SDL_GameControllerGetButton) &&
		SDL_GameControllerGetButton(m_private->m_gamecontroller, (SDL_GameControllerButton)button)))
	{
		return true;
	}
#endif
	return false;
}
Пример #7
0
int DEV_Joystick::Connected(void)
{
#ifdef WITH_SDL
	if (m_isinit &&
		(SDL_CHECK(SDL_GameControllerGetAttached) &&
		SDL_GameControllerGetAttached(m_private->m_gamecontroller)))
	{
		return 1;
	}
#endif
	return 0;
}
Пример #8
0
void DEV_Joystick::Close()
{
#ifdef WITH_SDL
	/* Closing possible connected Joysticks */
	for (int i = 0; i < JOYINDEX_MAX; i++) {
		m_instance[i]->ReleaseInstance(i);
	}

	/* Closing SDL Game controller system */
	if (SDL_CHECK(SDL_QuitSubSystem)) {
		SDL_QuitSubSystem(SDL_INIT_GAMECONTROLLER | SDL_INIT_HAPTIC);
}
#endif
}
Пример #9
0
bool DEV_Joystick::aAnyButtonPressIsPositive(void)
{
#ifdef WITH_SDL
	if (!(SDL_CHECK(SDL_GameControllerGetButton))) {
		return false;
	}

	/* this is needed for the "all events" option
	 * so we know if there are no buttons pressed */
	for (int i = 0; i < m_buttonmax; i++) {
		if (SDL_GameControllerGetButton(m_private->m_gamecontroller, (SDL_GameControllerButton)i)) {
			return true;
		}
	}
#endif
	return false;
}
Пример #10
0
bool DEV_Joystick::CreateJoystickDevice(void)
{
	bool joy_error = false;

#ifndef WITH_SDL
	m_isinit = true;
	joy_error = true;
#else /* WITH_SDL */
	if (!m_isinit) {

		if (!(SDL_CHECK(SDL_IsGameController) &&
			SDL_CHECK(SDL_GameControllerOpen) &&
			SDL_CHECK(SDL_GameControllerEventState) &&
			SDL_CHECK(SDL_GameControllerGetJoystick) &&
			SDL_CHECK(SDL_JoystickInstanceID)))
		{
			joy_error = true;
		}

		if (!joy_error && !SDL_IsGameController(m_joyindex)) {
			/* mapping instruccions if joystick is not a game controller */
			CM_Error("Game Controller index " << m_joyindex << ": Could not be initialized\n"
			<< "Please, generate Xbox360 compatible mapping using antimicro or Steam big mode application\n"
			<< "and after set, the SDL controller variable before you launch the executable, i.e:\n"
			<< "export SDL_GAMECONTROLLERCONFIG=\"[the string you received from controllermap]\"");
			/* Need this so python args can return empty lists */
			joy_error = true;
		}

		if (!joy_error) {
			m_private->m_gamecontroller = SDL_GameControllerOpen(m_joyindex);
			if (!m_private->m_gamecontroller) {
				joy_error = true;
			}
		}

		SDL_Joystick *joy;
		if (!joy_error) {
			joy = SDL_GameControllerGetJoystick(m_private->m_gamecontroller);
			if (!joy) {
				joy_error = true;
			}
		}

		if (!joy_error) {
			m_private->m_instance_id = SDL_JoystickInstanceID(joy);
			if (m_private->m_instance_id < 0){
				joy_error = true;
				CM_Error("joystick instanced failed: " << SDL_GetError());
			}
		}

		if (!joy_error) {
			CM_Debug("Game Controller (" << GetName() << ") with index " << m_joyindex << " initialized");

			/* A Game Controller has:
			 *
			 * 6 axis availables:	   AXIS_LEFTSTICK_X, AXIS_LEFTSTICK_Y,
			 * (in order from 0 to 5)  AXIS_RIGHTSTICK_X, AXIS_RIGHTSTICK_Y,
			 *						   AXIS_TRIGGERLEFT and AXIS_TRIGGERRIGHT.
			 *
			 * 15 buttons availables:  BUTTON_A, BUTTON_B, BUTTON_X, BUTTON_Y,
			 * (in order from 0 to 14) BUTTON_BACK, BUTTON_GUIDE, BUTTON_START,
			 *						   BUTTON_LEFTSTICK, BUTTON_RIGHTSTICK,
			 *						   BUTTON_LEFTSHOULDER, BUTTON_RIGHTSHOULDER,
			 *						   BUTTON_DPAD_UP, BUTTON_DPAD_DOWN,
			 *						   BUTTON_DPAD_LEFT and BUTTON_DPAD_RIGHT.
			 */
			m_axismax = SDL_CONTROLLER_AXIS_MAX;
			m_buttonmax = SDL_CONTROLLER_BUTTON_MAX;
		}

		/* Haptic configuration */
		if (!joy_error && SDL_CHECK(SDL_HapticOpen)) {
			m_private->m_haptic = SDL_HapticOpen(m_joyindex);
			if (!m_private->m_haptic) {
				CM_Warning("Game Controller (" << GetName() << ") with index " << m_joyindex
					<< " has not force feedback (vibration) available");
			}
		}
	}
#endif /* WITH_SDL */

	if (joy_error) {
		m_axismax = m_buttonmax = 0;
		return false;
	}
	else {
		m_isinit = true;
		return true;
	}
}
Пример #11
0
extern SkinSurface*
skin_surface_create_window(int x,
                           int y,
                           int w,
                           int h,
                           int original_w,
                           int original_h,
                           int is_fullscreen)
{
    static SkinSurface* result = NULL;

    SDL_Window* window = skin_winsys_get_window();
    SDL_Renderer* renderer = globals_get_renderer();

    D("%s: x=%d y=%d w=%d h=%d original_w=%d original_h=%d is_fullscreen=%d",
      __FUNCTION__, x, y, w, h, original_w, original_h, is_fullscreen);

    // Textures do not survive window resize events, so
    globals_foreach_surface(&_skin_surface_destroy_texture, NULL);

    if (!window) {
        // NOTE: Don't use SDL_WINDOW_ALLOW_HIGHDPI here. On OS X, this will
        //       make mouse event coordinates twice smaller than needed
        //       when running on a high-dpi machine (e.g. recent MacBook Pro),
        //       making the UI unusable. Note that this doesn't happen on a
        //       'low-dpi' device such as a MacPro connected to a 30" monitor.
        int window_flags = SDL_WINDOW_OPENGL;
        if (is_fullscreen) {
            window_flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
        }
        window = SDL_CreateWindow("Android emulator",
                                  x, y, w, h,
                                  window_flags);
        if (!window) {
            panic("Could not create SDL2 window: %s\n", SDL_GetError());
        }
        skin_winsys_set_window(window);
    } else {
        if (is_fullscreen) {
            SDL_CHECK(SDL_SetWindowFullscreen(window,
                                              SDL_WINDOW_FULLSCREEN_DESKTOP));
#if DEBUG
#endif
        } else {
            SDL_CHECK(SDL_SetWindowFullscreen(window, 0));
            SDL_SetWindowPosition(window, x, y);
            SDL_SetWindowSize(window, w, h);
        }
    }

    // Generate renderer
    if (!renderer) {
        SDL_CHECK(SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear"));
        renderer = SDL_CreateRenderer(window,
                                    -1,
                                    SDL_RENDERER_ACCELERATED|
                                    SDL_RENDERER_PRESENTVSYNC|
                                    SDL_RENDERER_TARGETTEXTURE);
        if  (!renderer) {
            panic("Could not create renderer: %s\n", SDL_GetError());
        }
        globals_set_renderer(renderer);
    }

    SDL_CHECK(SDL_RenderSetLogicalSize(renderer, original_w, original_h));

    if (DEBUG && VERBOSE_CHECK(surface)) {
        SDL_RendererInfo info;
        SDL_CHECK(SDL_GetRendererInfo(renderer, &info));
        printf("renderer.name = %s\n", info.name);
        printf("renderer.flags = 0x%x\n", info.flags);
        printf("renderer.num_texture_formats = %d\n", info.num_texture_formats);
        int nn;
        for (nn = 0; nn < info.num_texture_formats; ++nn) {
            printf("   0x%x (%s)\n", info.texture_formats[nn],
                   SDL_GetPixelFormatName(info.texture_formats[nn]));
        }
        printf("renderer.max_texture_width = %d\n", info.max_texture_width);
        printf("renderer.max_texture_height = %d\n", info.max_texture_height);
    }

    // Compute scaling parameters.
    {
        int window_x, window_y, window_w, window_h;

        SDL_GetWindowSize(window, &window_w, &window_h);
        SDL_GetWindowPosition(window, &window_x, &window_y);

        D("Window pos=(%d,%d) size=(%d,%d)", window_x, window_y, window_w, window_h);

        double x_scale = window_w * 1.0 / original_w;
        double y_scale = window_h * 1.0 / original_h;
        double scale = (x_scale <= y_scale) ? x_scale : y_scale;

        double effective_x = 0.;
        double effective_y = 0.;

        if (is_fullscreen) {
            effective_x = (window_w - original_w * scale) * 0.5;
            effective_y = (window_h - original_h * scale) * 0.5;
        }

        globals_set_window_scale(scale, effective_x, effective_y);
    }

    SDL_Texture* texture =
            SDL_CreateTexture(renderer,
                              SDL_PIXELFORMAT_ARGB8888,
                              SDL_TEXTUREACCESS_TARGET,
                              original_w,
                              original_h);
    if (!texture) {
        panic("Could not create window texture: %s", SDL_GetError());
    }

    SDL_CHECK(SDL_SetRenderTarget(renderer, texture));
    SDL_CHECK(SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255));
    SDL_CHECK(SDL_RenderClear(renderer));

    SDL_CHECK(SDL_SetRenderTarget(renderer, NULL));
    SDL_CHECK(SDL_SetRenderDrawColor(renderer, 0, 0, 128, 255));
    SDL_CHECK(SDL_RenderClear(renderer));
    SDL_CHECK(SDL_RenderCopy(renderer, texture, NULL, NULL));
    SDL_RenderPresent(renderer);

    if (!result) {
        result = _skin_surface_create(NULL, texture, original_w, original_h);
    } else {
        result->texture = texture;
        result->w = original_w;
        result->h = original_h;
    }
    skin_surface_ref(result);

    // Ensure all textures are regenerated properly.
    globals_foreach_surface(&_skin_surface_reset_texture, renderer);

    return result;
}