/** * @name context_2d_fillText * @brief fills text on the given context using given options * @param ctx - (context_2d *) ctx to fill text on * @param img - (texture_2d *) bitmap font texture to use * @param srcRect - (const rect_2d *) source rectangle on the texture to draw from * @param destRect - (const rect_2d *) destination rect to draw to * @param alpha - (float) alpha to draw with * @param composite_op - (int) composite operation to draw with * @retval NONE */ void context_2d_fillText(context_2d *ctx, texture_2d *img, const rect_2d *srcRect, const rect_2d *destRect, float alpha) { context_2d_bind(ctx); if (img && img->loaded) { draw_textures_item(ctx, GET_MODEL_VIEW_MATRIX(ctx), img->name, img->width, img->height, img->originalWidth, img->originalHeight, *srcRect, *destRect, *GET_CLIPPING_BOUNDS(ctx), ctx->globalAlpha[ctx->mvp] * alpha, ctx->globalCompositeOperation[ctx->mvp], &ctx->filter_color, ctx->filter_type); } }
/** * @name context_2d_drawImage * @brief darws an image to the given context using given options * @param ctx - (context_2d *) context to draw to * @param srcTex - (int) deprecated * @param url - (const char *) name of the texture to draw from * @param srcRect - (const rect_2d *) source rectangle on the texture to draw from * @param destRect - (const rect_2d *) destination rect to draw to * @param composite_op - (int) composite operation to draw with * @retval NONE */ void context_2d_drawImage(context_2d *ctx, int srcTex, const char *url, const rect_2d *srcRect, const rect_2d *destRect) { context_2d_bind(ctx); texture_2d *tex = texture_manager_load_texture(texture_manager_get(), url); if (tex && tex->loaded) { draw_textures_item(ctx, GET_MODEL_VIEW_MATRIX(ctx), tex->name, tex->width, tex->height, tex->originalWidth, tex->originalHeight, *srcRect, *destRect, * GET_CLIPPING_BOUNDS(ctx), ctx->globalAlpha[ctx->mvp], ctx->globalCompositeOperation[ctx->mvp], &ctx->filter_color, ctx->filter_type); } }
/** * @name context_2d_draw_point_sprites * @brief Draws pointsprites using the given options (in batch along a line) * @param ctx - (context_2d *) context to draw to * @param url - (const char *) name of the texture to draw from * @param point_size - (float) point sprite size * @param step_size - (float) step size to take between drawn pointsprites * @param color - (rgba *) color to draw with * @param x1 - (float) starting x-coordinate to draw along * @param y1 - (float) starting y-coordinate to draw along * @param x2 - (float) ending x-coordinate to draw along * @param y2 - (float) ending y-coordinate to draw along * @retval NONE */ void context_2d_draw_point_sprites(context_2d *ctx, const char *url, float point_size, float step_size, rgba *color, float x1, float y1, float x2, float y2) { draw_textures_flush(); context_2d_bind(ctx); texture_2d *tex = texture_manager_load_texture(texture_manager_get(), url); // If texture is not finished loading, if (!tex || !tex->loaded) { return; } static GLfloat *vertex_buffer = NULL; static unsigned int vertex_max = 64; tealeaf_shaders_bind(DRAWING_SHADER); matrix_3x3_multiply_m_f_f_f_f(GET_MODEL_VIEW_MATRIX(ctx), x1, y1, &x1, &y1); matrix_3x3_multiply_m_f_f_f_f(GET_MODEL_VIEW_MATRIX(ctx), x2, y2, &x2, &y2); // Allocate vertex array buffer if (vertex_buffer == NULL) { vertex_buffer = malloc(vertex_max * 2 * sizeof(GLfloat)); } // Add points to the buffer so there are drawing points every X pixels unsigned int count = ceilf(sqrtf((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)) / step_size); if (count < 1) { count = 1; } unsigned int vertex_count = 0; unsigned int i; for (i = 0; i < count; ++i) { if (vertex_count == vertex_max) { vertex_max = 2 * vertex_max; vertex_buffer = realloc(vertex_buffer, vertex_max * 2 * sizeof(GLfloat)); } vertex_buffer[2 * vertex_count + 0] = x1 + (x2 - x1) * ((GLfloat)i / (GLfloat)count); vertex_buffer[2 * vertex_count + 1] = y1 + (y2 - y1) * ((GLfloat)i / (GLfloat)count); vertex_count += 1; } GLTRACE(glActiveTexture(GL_TEXTURE0)); GLTRACE(glBindTexture(GL_TEXTURE_2D, tex->name)); GLTRACE(glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA)); GLTRACE(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)); GLTRACE(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)); GLTRACE(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)); GLTRACE(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)); // Render the vertex array GLTRACE(glUniform1f(global_shaders[DRAWING_SHADER].point_size, point_size)); GLTRACE(glVertexAttribPointer(global_shaders[DRAWING_SHADER].vertex_coords, 2, GL_FLOAT, GL_FALSE, 0, (float *) vertex_buffer)); float alpha = color->a * ctx->globalAlpha[ctx->mvp]; GLTRACE(glUniform4f(global_shaders[DRAWING_SHADER].draw_color, alpha * color->r, alpha * color->g, alpha * color->b, alpha)); GLTRACE(glDrawArrays(GL_POINTS, 0, vertex_count)); tealeaf_shaders_bind(PRIMARY_SHADER); }
void context_2d_setTransform(context_2d *ctx, double m11, double m12, double m21, double m22, double dx, double dy) { context_2d_bind(ctx); matrix_3x3 *m = GET_MODEL_VIEW_MATRIX(ctx); m->m00 = m11; m->m01 = m21; m->m10 = m12; m->m11 = m22; m->m02 = dx; m->m12 = dy; }
/** * @name context_2d_clearRect * @brief clears the given rect on the given context * @param ctx - (context_2d *) context to clear a rect from * @param rect - (const rect_2d *) rect to clear from the context * @retval NONE */ void context_2d_clearRect(context_2d *ctx, const rect_2d *rect) { draw_textures_flush(); context_2d_bind(ctx); // Draw a rectangle using triangle strip: // (0,1)-(2,3)-(4,5) and (2,3)-(4,5)-(6,7) // // With coordinates: // 4,5 - 6,7 // | \ | // 0,1 - 2,3 GLfloat v[8]; matrix_3x3_multiply(GET_MODEL_VIEW_MATRIX(ctx), rect, (float *)&v[4], (float *)&v[5], (float *)&v[6], (float *)&v[7], (float *)&v[2], (float *)&v[3], (float *)&v[0], (float *)&v[1]); glBlendFunc(GL_ONE, GL_ZERO); glUniform4f(global_shaders[PRIMARY_SHADER].draw_color, 0, 0, 0, 0); // set color to 0 glVertexAttribPointer(global_shaders[PRIMARY_SHADER].vertex_coords, 2, GL_FLOAT, GL_FALSE, 0, v); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); }
/** * @name context_2d_fillRect * @brief fills a rectangle on the given context using given options * @param ctx - (context_2d *) context to fill a rectangle on * @param rect - (const rect_2d *) rect to be filled * @param color - (const rgba *) color to fill with * @param composite_op - deprecated * @retval NONE */ void context_2d_fillRect(context_2d *ctx, const rect_2d *rect, const rgba *color) { if (use_single_shader) { return; } draw_textures_flush(); context_2d_bind(ctx); tealeaf_shaders_bind(FILL_RECT_SHADER); apply_composite_operation(ctx->globalCompositeOperation[ctx->mvp]); rect_2d_vertices in, out; rect_2d_to_rect_2d_vertices(rect, &in); matrix_3x3_multiply_m_r_r(GET_MODEL_VIEW_MATRIX(ctx), &in, &out); float alpha = color->a * ctx->globalAlpha[ctx->mvp]; // TODO: will pre-multiplied alpha cause a loss-of-precision in color for filling rectangles? GLTRACE(glUniform4f(global_shaders[FILL_RECT_SHADER].draw_color, alpha * color->r, alpha * color->g, alpha * color->b, alpha)); GLTRACE(glVertexAttribPointer(global_shaders[FILL_RECT_SHADER].vertex_coords, 2, GL_FLOAT, GL_FALSE, 0, &out)); GLTRACE(glDrawArrays(GL_TRIANGLE_STRIP, 0, 4)); tealeaf_shaders_bind(PRIMARY_SHADER); }
/** * @name context_2d_clear * @brief * @param ctx - (context_2d *) * @retval NONE */ void context_2d_clear(context_2d *ctx) { draw_textures_flush(); context_2d_bind(ctx); glClearColor(0, 0, 0, 0); glClear(GL_COLOR_BUFFER_BIT); }