Browse Source

-A little More control about pixel snapping in 2D

Juan Linietsky 10 years ago
parent
commit
79e5ced7e6
6 changed files with 18 additions and 3 deletions
  1. 1 0
      core/os/os.cpp
  2. 2 1
      core/os/os.h
  3. 2 2
      drivers/gles2/rasterizer_gles2.cpp
  4. 1 0
      main/main.cpp
  5. 5 0
      scene/2d/animated_sprite.cpp
  6. 7 0
      scene/2d/sprite.cpp

+ 1 - 0
core/os/os.cpp

@@ -516,6 +516,7 @@ OS::OS() {
 	_target_fps=0;
 	_render_thread_mode=RENDER_THREAD_SAFE;
 	_time_scale=1.0;
+	_pixel_snap=false;
 	Math::seed(1234567);
 }
 

+ 2 - 1
core/os/os.h

@@ -58,6 +58,7 @@ class OS {
 	float _fps;
 	int _target_fps;
 	float _time_scale;
+	bool _pixel_snap;
 
 	char *last_error;
 
@@ -393,7 +394,7 @@ public:
 	void set_time_scale(float p_scale);
 	float get_time_scale() const;
 
-
+	_FORCE_INLINE_ bool get_use_pixel_snap() const { return _pixel_snap; }
 
 	OS();	
 	virtual ~OS();

+ 2 - 2
drivers/gles2/rasterizer_gles2.cpp

@@ -4145,7 +4145,7 @@ void RasterizerGLES2::begin_frame() {
 
 	//fragment_lighting=Globals::get_singleton()->get("rasterizer/use_fragment_lighting");
 #ifdef TOOLS_ENABLED
-	canvas_shader.set_conditional(CanvasShaderGLES2::USE_PIXEL_SNAP,GLOBAL_DEF("rasterizer/use_pixel_snap",false));
+	canvas_shader.set_conditional(CanvasShaderGLES2::USE_PIXEL_SNAP,GLOBAL_DEF("display/use_2d_pixel_snap",false));
 	shadow_filter=ShadowFilterTechnique(int(Globals::get_singleton()->get("rasterizer/shadow_filter")));
 #endif
 
@@ -10807,7 +10807,7 @@ void RasterizerGLES2::init() {
 	copy_shader.set_conditional(CopyShaderGLES2::USE_8BIT_HDR,!use_fp16_fb);
 	canvas_shader.set_conditional(CanvasShaderGLES2::USE_DEPTH_SHADOWS,read_depth_supported);
 
-	canvas_shader.set_conditional(CanvasShaderGLES2::USE_PIXEL_SNAP,GLOBAL_DEF("rasterizer/use_pixel_snap",false));
+	canvas_shader.set_conditional(CanvasShaderGLES2::USE_PIXEL_SNAP,GLOBAL_DEF("display/use_2d_pixel_snap",false));
 
 	npo2_textures_available=true;
 	//fragment_lighting=false;

+ 1 - 0
main/main.cpp

@@ -701,6 +701,7 @@ Error Main::setup(const char *execpath,int argc, char *argv[],bool p_second_phas
 	GLOBAL_DEF("display/resizable",video_mode.resizable);
 	GLOBAL_DEF("display/test_width",0);
 	GLOBAL_DEF("display/test_height",0);
+	OS::get_singleton()->_pixel_snap=GLOBAL_DEF("display/use_2d_pixel_snap",false);
 	if (rtm==-1) {
 		rtm=GLOBAL_DEF("render/thread_model",OS::RENDER_THREAD_SAFE);
 		if (rtm>=1) //hack for now

+ 5 - 0
scene/2d/animated_sprite.cpp

@@ -28,6 +28,8 @@
 /*************************************************************************/
 #include "animated_sprite.h"
 #include "scene/scene_string_names.h"
+#include "os/os.h"
+
 void AnimatedSprite::edit_set_pivot(const Point2& p_pivot) {
 
 	set_offset(p_pivot);
@@ -153,6 +155,9 @@ void AnimatedSprite::_notification(int p_what) {
 			if (centered)
 				ofs-=s/2;
 
+			if (OS::get_singleton()->get_use_pixel_snap()) {
+				ofs=ofs.floor();
+			}
 			Rect2 dst_rect(ofs,s);
 
 			if (hflip)

+ 7 - 0
scene/2d/sprite.cpp

@@ -30,6 +30,7 @@
 #include "core/core_string_names.h"
 #include "scene/scene_string_names.h"
 #include "scene/main/viewport.h"
+#include "os/os.h"
 
 void Sprite::edit_set_pivot(const Point2& p_pivot) {
 
@@ -85,6 +86,9 @@ void Sprite::_notification(int p_what) {
 			Point2 ofs=offset;
 			if (centered)
 				ofs-=s/2;
+			if (OS::get_singleton()->get_use_pixel_snap()) {
+				ofs=ofs.floor();
+			}
 
 			Rect2 dst_rect(ofs,s);
 
@@ -422,6 +426,9 @@ void ViewportSprite::_notification(int p_what) {
 			if (centered)
 				ofs-=s/2;
 
+			if (OS::get_singleton()->get_use_pixel_snap()) {
+				ofs=ofs.floor();
+			}
 			Rect2 dst_rect(ofs,s);
 			texture->draw_rect_region(ci,dst_rect,src_rect,modulate);