Exemple #1
0
	void UIScrollBar::MouseOverHandler(UIDialog const & /*sender*/, uint32_t /*buttons*/, int2 const & pt)
	{
		last_mouse_ = pt;
		is_mouse_over_ = true;

		if (drag_)
		{
			thumb_rc_.bottom() += pt.y() - thumb_offset_y_ - thumb_rc_.top();
			thumb_rc_.top() = pt.y() - thumb_offset_y_;
			if (thumb_rc_.top() < track_rc_.top())
			{
				thumb_rc_ += int2(0, track_rc_.top() - thumb_rc_.top());
			}
			else
			{
				if (thumb_rc_.bottom() > track_rc_.bottom())
				{
					thumb_rc_ += int2(0, track_rc_.bottom() - thumb_rc_.bottom());
				}
			}

			// Compute first item index based on thumb position

			size_t nMaxFirstItem = end_ - start_ - page_size_;  // Largest possible index for first item
			size_t nMaxThumb = track_rc_.Height() - thumb_rc_.Height();  // Largest possible thumb position from the top

			position_ = start_ +
				(thumb_rc_.top() - track_rc_.top() +
				nMaxThumb / (nMaxFirstItem * 2)) * // Shift by half a row to avoid last row covered by only one pixel
				nMaxFirstItem  / nMaxThumb;
		}
	}
Exemple #2
0
void IGLUShaderVariable::operator= ( int2 val )
{
	// Check for a valid shader index
	if ( m_varIdx < 0 ) return;

	// Check for type mismatches
	if ( m_isAttribute )
	{
		AssignmentToAttribute( "ivec2" );
		return;
	}
	if ( m_varType != GL_INT_VEC2 && m_varType != GL_UNSIGNED_INT_VEC2 )
		TypeMismatch( "ivec2" );

	// Ensure this program is currently bound, or setting shader values fails!
	m_parent->PushProgram();

	// For types of variable that can be assigned from our input value, assign them here
	if ( m_varType == GL_INT_VEC2 )
		glUniform2iv( m_varIdx, 1, val.GetConstDataPtr() ); 
	if ( m_varType == GL_UNSIGNED_INT_VEC2 )
		glUniform2ui( m_varIdx, val.X(), val.Y() );

	// We have a short "program stack" so make sure to pop off.
	m_parent->PopProgram();
}	
	void UIPolylineEditBox::MouseUpHandler(UIDialog const & /*sender*/, uint32_t buttons, int2 const & pt)
	{
		if (buttons & MB_Left)
		{
			if (!this->GetDialog()->IsKeyboardInputEnabled())
			{
				this->GetDialog()->ClearFocus();
			}

			if (this->ContainsPoint(pt))
			{
				if (move_point_)
				{
					if ((this->ActivePoint() != 0) && (this->ActivePoint() != static_cast<int>(this->NumCtrlPoints() - 1)))
					{
						float2 p = this->PtFromCoord(pt.x(), pt.y());
						if ((p.x() < 0) || (p.x() > 1) || (p.y() < 0) || (p.y() > 1))
						{
							this->DelCtrlPoint(this->ActivePoint());
						}
					}

					move_point_ = false;
				}
			}
		}
	}
Exemple #4
0
void Set2DMode(const int2 &screensize)
{
    glDisable(GL_CULL_FACE);  
    glDisable(GL_DEPTH_TEST);

    otransforms = objecttransforms();

    view2clip = orthoGL(0, (float)screensize.x(), (float)screensize.y(), 0, 1, -1); // left handed coordinate system
}
Exemple #5
0
void OpenGLFrameStart(const int2 &screensize)
{
    glViewport(0, 0, screensize.x(), screensize.y());

    SetBlendMode(BLEND_ALPHA);

    curcolor = float4(1);

    lights.clear();
}
Exemple #6
0
	void UISlider::MouseDownHandler(UIDialog const & /*sender*/, uint32_t buttons, int2 const & pt)
	{
		if (buttons & MB_Left)
		{
			if (button_rc_.PtInRect(pt))
			{
				// Pressed while inside the control
				pressed_ = true;

				drag_x_ = pt.x();
				//m_nDragY = arg.location.y();
				drag_offset_ = button_x_ - drag_x_;

				//m_nDragValue = value_;

				if (!has_focus_)
				{
					this->GetDialog()->RequestFocus(*this);
				}

				return;
			}

			if (slider_rc_.PtInRect(pt))
			{
				drag_x_ = pt.x();
				drag_offset_ = 0;
				pressed_ = true;

				if (!has_focus_)
				{
					this->GetDialog()->RequestFocus(*this);
				}

				if (pt.x() > button_x_ + x_)
				{
					this->SetValueInternal(value_ + 1);
					return;
				}

				if (pt.x() < button_x_ + x_)
				{
					this->SetValueInternal(value_ - 1);
					return;
				}
			}
		}
	}
Exemple #7
0
	void UISlider::MouseOverHandler(UIDialog const & /*sender*/, uint32_t /*buttons*/, int2 const & pt)
	{
		if (pressed_)
		{
			this->SetValueInternal(ValueFromPos(x_ + pt.x() + drag_offset_));
		}
	}
Exemple #8
0
	void UIEditBox::MouseDownHandler(UIDialog const & /*sender*/, uint32_t buttons, int2 const & pt)
	{
		if (buttons & MB_Left)
		{
			if (!has_focus_)
			{
				this->GetDialog()->RequestFocus(*this);
			}

			if (this->ContainsPoint(pt))
			{
				mouse_drag_ = true;

				// Determine the character corresponding to the coordinates.
				int nX1st = buffer_.CPtoX(first_visible_, false);  // X offset of the 1st visible char
				bool bTrail;
				int nCP = buffer_.XtoCP(pt.x() - text_rc_.left() + nX1st, bTrail);

				// Cap at the nullptr character.
				if (bTrail && (nCP < static_cast<int>(buffer_.GetTextSize())))
				{
					this->PlaceCaret(nCP + 1);
				}
				else
				{
					this->PlaceCaret(nCP);
				}
				sel_start_ = caret_pos_;
				this->ResetCaretBlink();
			}
		}
	}
Exemple #9
0
//---------------------------------------------------------------------------------------------------------
BlockGenerator::BlockGenerator( const int2& size, int blockSize ) : mSize(size), mBockSize(blockSize)
{
	mNumBlocks = int2(
		(int)std::ceil(size.X() / (float) blockSize),
		(int)std::ceil(size.Y() / (float) blockSize));

	mBlocksLeft = mNumBlocks.X() * mNumBlocks.Y();

	mDirection = Right;

	mBlock = mNumBlocks / 2;

	mStepsLeft = 1;
	mNumSteps = 1;

	mStartTime = clock();
}
Exemple #10
0
	void UIScrollBar::MouseDownHandler(UIDialog const & /*sender*/, uint32_t buttons, int2 const & pt)
	{
		last_mouse_ = pt;
		if (buttons & MB_Left)
		{
			if (up_button_rc_.PtInRect(pt))
			{
				if (position_ > start_)
				{
					-- position_;
				}
				this->UpdateThumbRect();
				arrow_ = CLICKED_UP;
				arrow_ts_ = timer_.current_time();
				return;
			}

			// Check for click on down button

			if (down_button_rc_.PtInRect(pt))
			{
				if (position_ + page_size_ < end_)
				{
					++ position_;
				}
				this->UpdateThumbRect();
				arrow_ = CLICKED_DOWN;
				arrow_ts_ = timer_.current_time();
				return;
			}

			// Check for click on thumb

			if (thumb_rc_.PtInRect(pt))
			{
				drag_ = true;
				thumb_offset_y_ = pt.y() - thumb_rc_.top();
				return;
			}

			// Check for click on track

			if ((thumb_rc_.left() <= pt.x()) && (thumb_rc_.right() > pt.x()))
			{
				if ((thumb_rc_.top() > pt.y()) && (track_rc_.top() <= pt.y()))
				{
					this->Scroll(-static_cast<int>(page_size_ - 1));
				}
				else
				{
					if ((thumb_rc_.bottom() <= pt.y()) && (track_rc_.bottom() > pt.y()))
					{
						this->Scroll(static_cast<int>(page_size_ - 1));
					}
				}
			}
		}
	}
Exemple #11
0
bool ListBox::OnMouseButtonRelease( const int2& screenPos, uint32_t button )
{
	bool eventConsumed = false;

	if (button == MS_LeftButton)
	{
		if (mPressed)
		{
			if (mSelectionRegion.Contains((float)screenPos.X(), (float)screenPos.Y()))
			{
				int32_t selIndex = mVertScrollBar->GetScrollValue() + (int32_t)floorf((screenPos.Y() - mSelectionRegion.Top()) / mTextRowHeight);
				SetSelectedIndex(selIndex, true);			
			}	

			mPressed = false;
			eventConsumed = true;
		}
	}

	return eventConsumed;
}
Exemple #12
0
	void Blur::GaussBlur(
		const Ptr<ShaderResourceView> & src,
		const Ptr<RenderTargetView> & dst,
		int2 numSamples,
		float2 blurRadius)
	{
		auto dstTex = dst->GetResource()->Cast<Texture>();
		auto mipSize = dstTex->GetMipSize(dst->Cast<TextureRenderTargetView>()->mipLevel);

		TextureDesc texDesc = dst->GetResource()->Cast<Texture>()->GetDesc();
		texDesc.width = mipSize.x();
		texDesc.height = mipSize.y();
		texDesc.depth = 1;
		texDesc.arraySize = 1;
		texDesc.mipLevels = 1;
		auto tmpTex = TexturePool::Instance().FindFree({ TEXTURE_2D, texDesc });

		GaussBlurX(src, tmpTex->Get()->Cast<Texture>()->GetRenderTargetView(0, 0, 1), numSamples.x(), blurRadius.x());

		GaussBlurY(tmpTex->Get()->Cast<Texture>()->GetShaderResourceView(0, 1, 0, 1), dst, numSamples.y(), blurRadius.y());
	}
Exemple #13
0
float AADist(std::vector<float> const & img, std::vector<float2> const & grad,
	int width, int offset_addr, int2 const & offset_dist_xy, float2 const & new_dist)
{
	int closest = offset_addr - offset_dist_xy.y() * width - offset_dist_xy.x(); // Index to the edge pixel pointed to from c
	float val = img[closest];
	if (0 == val)
	{
		return 1e10f;
	}

	float di = MathLib::length(new_dist);
	float df;
	if (0 == di)
	{
		df = EdgeDistance(grad[closest], val);
	}
	else
	{
		df = EdgeDistance(new_dist, val);
	}
	return di + df;
}
Exemple #14
0
	void UIListBox::MouseOverHandler(UIDialog const & sender, uint32_t buttons, int2 const & pt)
	{
		// Let the scroll bar handle it first.
		scroll_bar_.MouseOverHandler(sender, buttons, pt);

		if (drag_)
		{
			// Compute the index of the item below cursor

			int nItem;
			if (text_height_ != 0)
			{
				nItem = static_cast<int>(scroll_bar_.GetTrackPos() + (pt.y() - text_rc_.top()) / text_height_);
			}
			else
			{
				nItem = -1;
			}

			// Only proceed if the cursor is on top of an item.

			if ((nItem >= static_cast<int>(scroll_bar_.GetTrackPos()))
				&& (nItem < static_cast<int>(items_.size()))
				&& (nItem < static_cast<int>(scroll_bar_.GetTrackPos() + scroll_bar_.GetPageSize())))
			{
				selected_ = nItem;
				this->OnSelectionEvent()(*this);
			}
			else
			{
				if (nItem < static_cast<int>(scroll_bar_.GetTrackPos()))
				{
					// User drags the mouse above window top
					scroll_bar_.Scroll(-1);
					selected_ = static_cast<int>(scroll_bar_.GetTrackPos());
					this->OnSelectionEvent()(*this);
				}
				else
				{
					if (nItem >= static_cast<int>(scroll_bar_.GetTrackPos() + scroll_bar_.GetPageSize()))
					{
						// User drags the mouse below window bottom
						scroll_bar_.Scroll(1);
						selected_ = static_cast<int>(std::min(items_.size(), scroll_bar_.GetTrackPos() + scroll_bar_.GetPageSize())) - 1;
						this->OnSelectionEvent()(*this);
					}
				}
			}
		}
	}
	void UIPolylineEditBox::MouseDownHandler(UIDialog const & /*sender*/, uint32_t buttons, int2 const & pt)
	{
		if (buttons & MB_Left)
		{
			if (!has_focus_)
			{
				this->GetDialog()->RequestFocus(*this);
			}

			if (this->ContainsPoint(pt))
			{
				float2 p = this->PtFromCoord(pt.x(), pt.y());
				if (MathLib::abs(this->GetValue(p.x()) - p.y()) < 0.1f)
				{
					bool found = false;
					for (int i = 0; i < static_cast<int>(this->NumCtrlPoints()); ++ i)
					{
						float2 cp = this->GetCtrlPoint(i);
						cp = p - cp;
						if (MathLib::dot(cp, cp) < 0.01f)
						{
							this->ActivePoint(i);
							move_point_ = true;
							found = true;
							break;
						}
					}

					if (!found)
					{
						this->AddCtrlPoint(p.x());
						move_point_ = true;
					}
				}
			}
		}
	}
Exemple #16
0
	void UIEditBox::MouseOverHandler(UIDialog const & /*sender*/, uint32_t /*buttons*/, int2 const & pt)
	{
		if (mouse_drag_)
		{
			// Determine the character corresponding to the coordinates.
			int nX1st = buffer_.CPtoX(first_visible_, false);  // X offset of the 1st visible char
			bool bTrail;
			int nCP = buffer_.XtoCP(pt.x() - text_rc_.left() + nX1st, bTrail);
			// Cap at the nullptr character.
			if (bTrail && (nCP < static_cast<int>(buffer_.GetTextSize())))
			{
				this->PlaceCaret(nCP + 1);
			}
			else
			{
				this->PlaceCaret(nCP);
			}
		}
	}
Exemple #17
0
bool ScreenShot(const char *filename)
{
    SDL_Surface *surf = SDL_CreateRGBSurface(0, screensize.x(), screensize.y(), 24,
                                             0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
    if (!surf) return false;

    auto pixels = ReadPixels(int2(0), screensize, false);
    SDL_LockSurface(surf);
    for (int i = 0; i < screensize.y(); i++)
        memcpy(((char *)surf->pixels) + surf->pitch * i,
               pixels + 3 * screensize.x() * (screensize.y() - i - 1),
               screensize.x() * 3);
    SDL_UnlockSurface(surf);
    delete[] pixels;

    bool ok = !SDL_SaveBMP(surf, filename);
    SDL_FreeSurface(surf);
    return ok;
}
Exemple #18
0
	void UIListBox::MouseDownHandler(UIDialog const & sender, uint32_t buttons, int2 const & pt)
	{
		if (buttons & MB_Left)
		{
			if (!has_focus_)
			{
				this->GetDialog()->RequestFocus(*this);
			}
		}

		// Let the scroll bar handle it first.
		scroll_bar_.MouseDownHandler(sender, buttons, pt);

		if (buttons & MB_Left)
		{
			// Check for clicks in the text area
			if (!items_.empty() && selection_rc_.PtInRect(pt))
			{
				// Compute the index of the clicked item

				int nClicked;
				if (text_height_ != 0)
				{
					nClicked = static_cast<int>(scroll_bar_.GetTrackPos() + (pt.y() - text_rc_.top()) / text_height_);
				}
				else
				{
					nClicked = -1;
				}

				// Only proceed if the click falls on top of an item.

				if ((nClicked >= static_cast<int>(scroll_bar_.GetTrackPos()))
					&& (nClicked < static_cast<int>(items_.size()))
					&& (nClicked < static_cast<int>(scroll_bar_.GetTrackPos() + scroll_bar_.GetPageSize())))
				{
					drag_ = true;

					selected_ = nClicked;
					if (!(buttons & MB_Shift))
					{
						sel_start_ = selected_;
					}

					// If this is a multi-selection listbox, update per-item
					// selection data.

					if (MULTI_SELECTION == style_)
					{
						// Determine behavior based on the state of Shift and Ctrl

						std::shared_ptr<UIListBoxItem> const & pSelItem = items_[selected_];
						if (MB_Ctrl == (buttons & (MB_Shift | MB_Ctrl)))
						{
							// Control click. Reverse the selection of this item.

							pSelItem->bSelected = !pSelItem->bSelected;
						}
						else
						{
							if (MB_Shift == (buttons & (MB_Shift | MB_Ctrl)))
							{
								// Shift click. Set the selection for all items
								// from last selected item to the current item.
								// Clear everything else.

								int nBegin = std::min(sel_start_, selected_);
								int nEnd = std::max(sel_start_, selected_);

								for (int i = 0; i < nBegin; ++ i)
								{
									items_[i]->bSelected = false;
								}

								for (int i = nEnd + 1; i < static_cast<int>(items_.size()); ++ i)
								{
									items_[i]->bSelected = false;
								}

								for (int i = nBegin; i <= nEnd; ++ i)
								{
									items_[i]->bSelected = true;
								}
							}
							else
							{
								if ((MB_Shift | MB_Ctrl) == (buttons & (MB_Shift | MB_Ctrl)))
								{
									// Control-Shift-click.

									// The behavior is:
									//   Set all items from sel_start_ to selected_ to
									//     the same state as sel_start_, not including selected_.
									//   Set selected_ to selected.

									int nBegin = std::min(sel_start_, selected_);
									int nEnd = std::max(sel_start_, selected_);

									// The two ends do not need to be set here.

									bool bLastSelected = items_[sel_start_]->bSelected;
									for (int i = nBegin + 1; i < nEnd; ++ i)
									{
										items_[i]->bSelected = bLastSelected;
									}

									pSelItem->bSelected = true;

									// Restore selected_ to the previous value
									// This matches the Windows behavior

									selected_ = sel_start_;
								}
								else
								{
									// Simple click.  Clear all items and select the clicked
									// item.


									for (size_t i = 0; i < items_.size(); ++ i)
									{
										items_[i]->bSelected = false;
									}

									pSelItem->bSelected = true;
								}
							}
						}
					}  // End of multi-selection case

					this->OnSelectionEvent()(*this);
				}
			}
		}
	}
Exemple #19
0
 STARTDECL(gl_perspective) (Value &fovy, Value &znear, Value &zfar)
 {
     Set3DMode(fovy.fval*RAD, screensize.x() / (float)screensize.y(), znear.fval, zfar.fval);
     return Value();
 }
Exemple #20
0
int main()
{
  return *(ib.begin()->begin()) != 42;
}
	void UIPolylineEditBox::MouseOverHandler(UIDialog const & /*sender*/, uint32_t buttons, int2 const & pt)
	{
		if (move_point_ || this->ContainsPoint(pt))
		{
			if (0 == buttons)
			{
				float2 p = this->PtFromCoord(pt.x(), pt.y());
				if (MathLib::abs(this->GetValue(p.x()) - p.y()) < 0.1f)
				{
					elements_[POLYLINE_INDEX]->TextureColor().SetState(UICS_MouseOver);
				}
				else
				{
					elements_[POLYLINE_INDEX]->TextureColor().SetState(UICS_Normal);
				}

				int move_over_pt = -1;
				for (int i = 0; i < static_cast<int>(this->NumCtrlPoints()); ++ i)
				{
					float2 cp = this->GetCtrlPoint(i);
					cp = p - cp;
					if (MathLib::dot(cp, cp) < 0.01f)
					{
						move_over_pt = i;
						break;
					}
				}
				if (move_over_pt != -1)
				{
					active_pt_ = move_over_pt;
				}
			}
			else
			{
				if (buttons & MB_Left)
				{
					if (move_point_)
					{
						float2 p = this->PtFromCoord(pt.x(), pt.y());
						if (0 == this->ActivePoint())
						{
							p.x() = 0;
							p.y() = MathLib::clamp(p.y(), 0.0f, 1.0f);
						}
						else
						{
							if (static_cast<int>(this->NumCtrlPoints() - 1) == this->ActivePoint())
							{
								p.x() = 1;
								p.y() = MathLib::clamp(p.y(), 0.0f, 1.0f);
							}
						}
						if (this->ActivePoint() < static_cast<int>(this->NumCtrlPoints() - 1))
						{
							float2 next_p = this->GetCtrlPoint(this->ActivePoint() + 1);
							if (next_p.x() <= p.x())
							{
								p.x() = next_p.x();
							}
						}
						this->SetCtrlPoint(this->ActivePoint(), p.x(), p.y());
					}
				}
			}
		}
	}
Exemple #22
0
string SDLInit(const char *title, int2 &screensize, bool fullscreen)
{
    //SDL_SetMainReady();
    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER /* | SDL_INIT_AUDIO*/) < 0)
    {
        return SDLError("Unable to initialize SDL");
    }

    SDL_SetEventFilter(SDLHandleAppEvents, nullptr);

    DebugLog(-1, "SDL initialized...");

    SDL_LogSetAllPriority(SDL_LOG_PRIORITY_WARN);

    // on demand now
    //extern bool sfxr_init();
    //if (!sfxr_init())
    //   return SDLError("Unable to initialize audio");

    #ifdef PLATFORM_MOBILE
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
    #else
    //certain older Intel HD GPUs and also Nvidia Quadro 1000M don't support 3.1 ? the 1000M is supposed to support 4.2
    //SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
    //SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
    #ifndef WIN32
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG);
    #endif
    SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
    SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4);
    #endif

    //SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0);      // set this if we're in 2D mode for speed on mobile?
    SDL_GL_SetAttribute(SDL_GL_RETAINED_BACKING, 1);    // because we redraw the screen each frame

    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

    DebugLog(-1, "SDL about to figure out display mode...");

    #ifdef PLATFORM_MOBILE
    landscape = screensize.x() >= screensize.y();
    int modes = SDL_GetNumDisplayModes(0);
    screensize = int2(0);
    for (int i = 0; i < modes; i++)
    {
        SDL_DisplayMode mode;
        SDL_GetDisplayMode(0, i, &mode);
        //printf("mode: %d %d\n", mode.w, mode.h);
        if (landscape ? mode.w > screensize.x() : mode.h > screensize.y())
        {
            screensize = int2(mode.w, mode.h);
        }
    }

    DebugLog(-1, inttoa(screensize.x()));
    DebugLog(-1, inttoa(screensize.y()));
    DebugLog(-1, "SDL about to create window...");

    _sdl_window = SDL_CreateWindow(title,
                                    0, 0,
                                    screensize.x(), screensize.y(),
                                    SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_BORDERLESS);

    DebugLog(-1, _sdl_window ? "SDL window passed..." : "SDL window FAILED...");

    if (landscape) SDL_SetHint("SDL_HINT_ORIENTATIONS", "LandscapeLeft LandscapeRight");

    int ax = 0, ay = 0;
    SDL_GetWindowSize(_sdl_window, &ax, &ay);
    int2 actualscreensize(ax, ay);
    //screenscalefactor = screensize.x / actualscreensize.x;  // should be 2 on retina
    #ifdef __IOS__
        assert(actualscreensize == screensize);
        screensize = actualscreensize;
    #else
        screensize = actualscreensize;  // __ANDROID__
        DebugLog(-1, inttoa(screensize.x()));
        DebugLog(-1, inttoa(screensize.y()));
    #endif
    #else
    _sdl_window = SDL_CreateWindow(title,
                                    SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
                                    screensize.x(), screensize.y(),
                                    SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE |
                                        (fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0));
    #endif

    if (!_sdl_window)
        return SDLError("Unable to create window");

    DebugLog(-1, "SDL window opened...");


    _sdl_context = SDL_GL_CreateContext(_sdl_window);
    DebugLog(-1, _sdl_context ? "SDL context passed..." : "SDL context FAILED...");
    if (!_sdl_context) return SDLError("Unable to create OpenGL context");

    DebugLog(-1, "SDL OpenGL context created...");

    /*
    SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
    SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4);
    */

    #ifndef __IOS__
        SDL_GL_SetSwapInterval(1);  // vsync on
    #endif

    SDL_JoystickEventState(SDL_ENABLE);
    SDL_JoystickUpdate();
    for(int i = 0; i < SDL_NumJoysticks(); i++)
    {
        SDL_Joystick *joy;
        if (joy = SDL_JoystickOpen(i))
        {
            DebugLog(-1, "Detected joystick: %s (%d axes, %d buttons, %d balls, %d hats)\n",
                         SDL_JoystickName(joy), SDL_JoystickNumAxes(joy), SDL_JoystickNumButtons(joy),
                         SDL_JoystickNumBalls(joy), SDL_JoystickNumHats(joy));
        };
    };

    starttime = SDL_GetTicks();
    lastmillis = starttime - 16;    // ensure first frame doesn't get a crazy delta

    return "";
}
Exemple #23
0
int main(int argc, char* argv[]) {
  const int2 videoSize = int2(1280, 720);
  const cl_uint    maxDevices = 8;
  cl_device_id     computeDevices[maxDevices];
  cl_context       context = NULL;
  cl_command_queue commandQueue = NULL;
  initOpenCL(computeDevices, maxDevices, context, commandQueue);
  checkCLerror(__LINE__, __FILE__);

  cl_program program;
  vector<const char*> paths = {"03_sharedWithCL.h", "UIshader.cl"};
  initClProgram(paths, program, context, computeDevices);
  checkCLerror(__LINE__, __FILE__);
  cl_kernel kernel = clCreateKernel(program, "UIshader", &CLstatus);
  checkCLerror(__LINE__, __FILE__);

  cl_mem outputImage;
  {
    cl_image_format outputImageFormat = {CL_RGBA, CL_UNORM_INT8};
    cl_image_desc outputImageDesc;
    memset(&outputImageDesc, '\0', sizeof(cl_image_desc));
    outputImageDesc.image_type   = CL_MEM_OBJECT_IMAGE2D;
    outputImageDesc.image_width  = videoSize.x;
    outputImageDesc.image_height = videoSize.y;
    outputImage = clCreateImage(
      context,               //cl_context             context,
      CL_MEM_WRITE_ONLY,     //cl_mem_flags           flags,
      &outputImageFormat,    //const cl_image_format *image_format,
      &outputImageDesc,      //const cl_image_desc   *image_desc,
      NULL,                  //void                  *host_ptr,
      &CLstatus              //cl_int                *errcode_ret
    );
    checkCLerror(__LINE__, __FILE__);
  }


  initGlyphSheet("glyphSheets/hermit_010_023");
  {
    cl_image_format glyphSheetFormat = {CL_LUMINANCE, CL_UNORM_INT8};
    cl_image_desc glyphSheetDesc;
    memset(&glyphSheetDesc, '\0', sizeof(cl_image_desc));
    glyphSheetDesc.image_type   = CL_MEM_OBJECT_IMAGE2D;
    glyphSheetDesc.image_width  = gss->w;
    glyphSheetDesc.image_height = gss->h;
    glyphSheet = clCreateImage(
      context,               //cl_context             context,
      CL_MEM_READ_ONLY,      //cl_mem_flags           flags,
      &glyphSheetFormat,     //const cl_image_format *image_format,
      &glyphSheetDesc,       //const cl_image_desc   *image_desc,
      NULL,                  //void                  *host_ptr,
      &CLstatus              //cl_int                *errcode_ret
    );
    checkCLerror(__LINE__, __FILE__);
  }
  {
    size_t origin[] = {0,0,0};
    size_t region[] = {(size_t)gss->w, (size_t)gss->h, 1};
    CLstatus = clEnqueueWriteImage(
      commandQueue,         //cl_command_queue  command_queue,
      glyphSheet,           //cl_mem            image,
      CL_TRUE,              //cl_bool           blocking_write,
      &origin[0],           //const size_t     *origin,
      &region[0],           //const size_t     *region,
      gss->w,               //size_t            input_row_pitch,
      0,                    //size_t            input_slice_pitch,
      gss->pixels,          //const void       *ptr,
      0,                    //cl_uint           num_events_in_wait_list,
      NULL,                 //const cl_event   *event_wait_list,
      NULL                  //cl_event         *event
    );
    checkCLerror(__LINE__, __FILE__);
  }

  buildsomeroots();

  size_t UItextSize = sizeof(int) * UItextBlock.size.pro();
  cl_mem UItext_clmem = clCreateBuffer(
    context,
    CL_MEM_READ_ONLY,
    UItextSize,
    NULL,
    &CLstatus
  );
  checkCLerror(__LINE__, __FILE__);
  CLstatus = clEnqueueWriteBuffer (
    commandQueue,                    //cl_command_queue command_queue,
    UItext_clmem,                    //cl_mem           buffer,
    CL_TRUE,                         //cl_bool          blocking_write,
    0,                               //size_t           offset,
    UItextSize,                      //size_t           cb,
    (void*)UItextBlock.text.data(),  //const void      *ptr,
    0,                               //cl_uint          num_events_in_wait_list,
    NULL,                            //const cl_event  *event_wait_list,
    NULL                             //cl_event        *event
  );
  checkCLerror(__LINE__, __FILE__);

  cl_mem gsi_clmem = clCreateBuffer(
    context,
    CL_MEM_READ_ONLY,
    UItextSize,
    NULL,
    &CLstatus
  );
  checkCLerror(__LINE__, __FILE__);
  CLstatus = clEnqueueWriteBuffer (
    commandQueue,             //cl_command_queue command_queue,
    gsi_clmem,                //cl_mem           buffer,
    CL_TRUE,                  //cl_bool          blocking_write,
    0,                        //size_t           offset,
    sizeof(glyphSheetInfo),   //size_t           cb,
    (void*)&gsi,              //const void      *ptr,
    0,                        //cl_uint          num_events_in_wait_list,
    NULL,                     //const cl_event  *event_wait_list,
    NULL                      //cl_event        *event
  );
  checkCLerror(__LINE__, __FILE__);

#if kernelInspectArgIndex
  vector<int> kernelInspect(videoSize.Pro());
  for (int i = 0; i < videoSize.Pro(); i++) kernelInspect[i] = 1234;
  cl_mem kernelInspect_clmem = clCreateBuffer(
    context,
    CL_MEM_WRITE_ONLY,
    sizeof(int)*videoSize.Pro(),
    NULL,
    &CLstatus
  );
  checkCLerror(__LINE__, __FILE__);
  CLstatus = clEnqueueWriteBuffer (
    commandQueue,                  //cl_command_queue command_queue,
    kernelInspect_clmem,           //cl_mem           buffer,
    CL_TRUE,                       //cl_bool          blocking_write,
    0,                             //size_t           offset,
    sizeof(int)*videoSize.Pro(),   //size_t           cb,
    (void*)kernelInspect.data(),   //const void      *ptr,
    0,                             //cl_uint          num_events_in_wait_list,
    NULL,                          //const cl_event  *event_wait_list,
    NULL                           //cl_event        *event
  );
  checkCLerror(__LINE__, __FILE__);
#endif

  CLstatus = clSetKernelArg(kernel, 1, sizeof(int2), (void*)&UItextBlock.size);
  checkCLerror(__LINE__, __FILE__);

  CLstatus = clSetKernelArg(kernel, 2, sizeof(cl_mem), (void*)&UItext_clmem);
  checkCLerror(__LINE__, __FILE__);
  CLstatus = clSetKernelArg(kernel, 3, sizeof(cl_mem), (void*)&gsi_clmem);
  checkCLerror(__LINE__, __FILE__);

  CLstatus = clSetKernelArg(kernel, 4, sizeof(cl_mem), (void*)&glyphSheet);
  checkCLerror(__LINE__, __FILE__);
  CLstatus = clSetKernelArg(kernel, 5, sizeof(cl_mem), (void*)&outputImage);
  checkCLerror(__LINE__, __FILE__);

#if kernelInspectArgIndex
  CLstatus = clSetKernelArg(
    kernel,
    kernelInspectArgIndex,
    sizeof(cl_mem),
    (void*)&kernelInspect_clmem
  );
  checkCLerror(__LINE__, __FILE__);
#endif


  SDL_Init(SDL_INIT_VIDEO);
  if (!strcmp(SDL_GetError(), "Unknown touch device")) SDL_ClearError();//??
  checkSDLerror(__LINE__, __FILE__);
  SDL_Window *window = SDL_CreateWindow(
    "glyphdraw",               //const char* title,
    SDL_WINDOWPOS_UNDEFINED,   //int         x,
    SDL_WINDOWPOS_UNDEFINED,   //int         y,
    videoSize.x,               //int         w,
    videoSize.y,               //int         h,
    0                          //Uint32      flags
  );
  checkSDLerror(__LINE__, __FILE__);
  SDL_Surface *windowSrfc = SDL_GetWindowSurface(window);
  if (!strcmp(SDL_GetError(), "Invalid renderer")) SDL_ClearError();//??
  checkSDLerror(__LINE__, __FILE__);

  {
    float  cursPress  = 0;
    float  pCursPress = 0;
    float2 cursPos;
    float2 pCursPos;
    float2 UItextBlockPixCount = UItextBlock.size * gsi.glyphSize;
    const float scrollAccel = 1.2;
    scrollable scroll = scrollable(scrollAccel, UItextBlockPixCount, videoSize);
    int curFrame = 0;
    bool running = true;
    while (running) {
      pCursPress = cursPress;
      pCursPos   = cursPos;
      SDL_Event event;
      while (SDL_PollEvent(&event)) {
        switch (event.type) {
          case SDL_QUIT: running = false; break;
          case SDL_WINDOWEVENT:
            if (event.window.event == SDL_WINDOWEVENT_EXPOSED) {
              SDL_UpdateWindowSurface(window);
              checkSDLerror(__LINE__, __FILE__);
            }
            break;
          case SDL_MOUSEMOTION:
            cursPos = float2(event.motion.x, event.motion.y);
            break;
          case SDL_MOUSEBUTTONDOWN:
            switch (event.button.button) {
              case SDL_BUTTON_LEFT: cursPress = 1; break;
            }
            break;
          case SDL_MOUSEBUTTONUP:
            switch (event.button.button) {
              case SDL_BUTTON_LEFT: cursPress = 0; break;
            }
            break;
        }
      }
      scroll.advance(cursPress, pCursPress, cursPos, pCursPos);
      if (scroll.hasMoved() || !curFrame) {

        int2 offset = int2(scroll.getPos().x, scroll.getPos().y);
        CLstatus = clSetKernelArg(kernel, 0, sizeof(int2), (void*)&offset);
        checkCLerror(__LINE__, __FILE__);

        size_t globalWorkSize[] = {(size_t)videoSize.x, (size_t)videoSize.y};
        CLstatus = clEnqueueNDRangeKernel(
          commandQueue,       //cl_command_queue command_queue,
          kernel,             //cl_kernel        kernel,
          2,                  //cl_uint          work_dim,
          NULL,               //const size_t    *global_work_offset,
          globalWorkSize,     //const size_t    *global_work_size,
          NULL,               //const size_t    *local_work_size,
          0,                  //cl_uint          num_events_in_wait_list,
          NULL,               //const cl_event  *event_wait_list,
          NULL                //cl_event        *event
        );
        checkCLerror(__LINE__, __FILE__);

        size_t origin[] = {0, 0, 0};
        size_t region[] = {(size_t)videoSize.x, (size_t)videoSize.y, 1};
        CLstatus = clEnqueueReadImage(
          commandQueue,       //cl_command_queue command_queue,
          outputImage,        //cl_mem           image,
          CL_TRUE,            //cl_bool          blocking_read,
          origin,             //const            size_t origin[3],
          region,             //const            size_t region[3],
          0,                  //size_t           row_pitch,
          0,                  //size_t           slice_pitch,
          windowSrfc->pixels, //void            *ptr,
          0,                  //cl_uint          num_events_in_wait_list,
          NULL,               //const cl_event  *event_wait_list,
          NULL                //cl_event        *event
        );
        checkCLerror(__LINE__, __FILE__);


        SDL_UpdateWindowSurface(window);
        checkSDLerror(__LINE__, __FILE__);

#if kernelInspectArgIndex
        CLstatus = clEnqueueReadBuffer(
          commandQueue,                 //cl_command_queue command_queue,
          kernelInspect_clmem,          //cl_mem           buffer,
          CL_TRUE,                      //cl_bool          blocking_read,
          0,                            //size_t           offset,
          sizeof(int)*videoSize.Pro(),  //size_t           cb,
          kernelInspect.data(),         //void            *ptr,
          0,                            //cl_uint          num_events_in_wait_list,
          NULL,                         //const cl_event  *event_wait_list,
          NULL                          //cl_event        *event
        );
        checkCLerror(__LINE__, __FILE__);
        cout << endl << endl << "glyphSheetPos.y" << endl << endl;
        for (int row = 0; row < 3; row++) {
          cout << endl << endl << "row: " << row << endl;
          for (
            int i = videoSize.x * gsi.glyphSize.y * row;
            i < videoSize.x * gsi.glyphSize.y * row + gss->w;
            i += gsi.glyphSize.x
          ) {
            cout << "kernelInspect[" << i << "]: " << kernelInspect[i] << endl;
          }
        }
#endif
      }
      SDL_Delay(10);
      curFrame++;
    }
  }

  CLstatus = clReleaseKernel(kernel);
  checkCLerror(__LINE__, __FILE__);
  CLstatus = clReleaseProgram(program);
  checkCLerror(__LINE__, __FILE__);
  CLstatus = clReleaseMemObject(UItext_clmem);
  checkCLerror(__LINE__, __FILE__);
  CLstatus = clReleaseMemObject(gsi_clmem);
  checkCLerror(__LINE__, __FILE__);
  CLstatus = clReleaseMemObject(glyphSheet);
  checkCLerror(__LINE__, __FILE__);
  CLstatus = clReleaseMemObject(outputImage);
  checkCLerror(__LINE__, __FILE__);
#if kernelInspectArgIndex
  CLstatus = clReleaseMemObject(kernelInspect_clmem);
  checkCLerror(__LINE__, __FILE__);
#endif
  CLstatus = clReleaseCommandQueue(commandQueue);
  checkCLerror(__LINE__, __FILE__);
  CLstatus = clReleaseContext(context);
  checkCLerror(__LINE__, __FILE__);

  SDL_FreeSurface(gss);
  checkSDLerror(__LINE__, __FILE__);
  SDL_FreeSurface(windowSrfc);
  checkSDLerror(__LINE__, __FILE__);
  SDL_DestroyWindow(window);
  checkSDLerror(__LINE__, __FILE__);
  SDL_Quit();

  cout << "exited normally" << endl;
  exit(0);
}