Initial Commit
This commit is contained in:
commit
dc5aa88e90
19
LICENSE
Normal file
19
LICENSE
Normal file
@ -0,0 +1,19 @@
|
||||
MIT License
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above license notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
41
README.md
Normal file
41
README.md
Normal file
@ -0,0 +1,41 @@
|
||||
# nanote
|
||||
|
||||
nanote is a simple drawing application written in C++20 that uses Skia for graphics rendering and SDL2 for window creation and event handling. The application allows users to draw on the window using the cursor.
|
||||
|
||||
## Description
|
||||
|
||||
This application provides a basic canvas where users can draw using their cursor. It utilizes Skia, a powerful 2D graphics library, for rendering graphics, and SDL2, a popular cross-platform development library, for window creation and event handling.
|
||||
|
||||
## Usage
|
||||
|
||||
1. **Building the Project**:
|
||||
- Make sure you have Bazel installed on your system.
|
||||
- Navigate to the project directory.
|
||||
- Run the following command to build the project:
|
||||
|
||||
```bash
|
||||
bazel build //:skia_sdl2
|
||||
```
|
||||
|
||||
2. **Running the Application**:
|
||||
- After building the project, run the following command to execute the application:
|
||||
|
||||
```bash
|
||||
bazel-bin/skia_sdl2
|
||||
```
|
||||
|
||||
3. **Drawing on the Canvas**:
|
||||
- Once the application window opens, you can draw on the canvas using your cursor.
|
||||
|
||||
4. **Exiting the Application**:
|
||||
- To exit the application, simply close the window.
|
||||
|
||||
## Dependencies
|
||||
|
||||
- Skia: A powerful 2D graphics library.
|
||||
- SDL2: A cross-platform development library providing low-level access to audio, keyboard, mouse, and display.
|
||||
|
||||
## License
|
||||
|
||||
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
||||
|
19
build.bazel
Normal file
19
build.bazel
Normal file
@ -0,0 +1,19 @@
|
||||
cc_binary(
|
||||
name = "nanote",
|
||||
srcs = ["src/main.cpp"],
|
||||
copts = [
|
||||
"-std=c++20",
|
||||
],
|
||||
deps = [
|
||||
"@skia//:core",
|
||||
"@skia//:effects",
|
||||
"@skia//:encode",
|
||||
"@skia//:gpu",
|
||||
"@skia//:images",
|
||||
"@skia//:opts",
|
||||
"@skia//:svg",
|
||||
"@skia//:text",
|
||||
"@skia//:utils",
|
||||
"@sdl2//:sdl2",
|
||||
],
|
||||
)
|
83
src/main.cpp
Normal file
83
src/main.cpp
Normal file
@ -0,0 +1,83 @@
|
||||
#include <iostream>
|
||||
#include "SDL.h"
|
||||
#include "include/core/SkCanvas.h"
|
||||
#include "include/core/SkPaint.h"
|
||||
|
||||
constexpr int WINDOW_WIDTH = 640;
|
||||
constexpr int WINDOW_HEIGHT = 480;
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
// Initialize SDL
|
||||
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
|
||||
std::cerr << "SDL initialization failed: " << SDL_GetError() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Create SDL window
|
||||
SDL_Window* window = SDL_CreateWindow("Drawing Application", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN);
|
||||
if (!window) {
|
||||
std::cerr << "Failed to create SDL window: " << SDL_GetError() << std::endl;
|
||||
SDL_Quit();
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Create SDL renderer
|
||||
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
|
||||
if (!renderer) {
|
||||
std::cerr << "Failed to create SDL renderer: " << SDL_GetError() << std::endl;
|
||||
SDL_DestroyWindow(window);
|
||||
SDL_Quit();
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Initialize Skia
|
||||
sk_sp<SkSurface> surface = SkSurface::MakeRenderTargetDirect(SkiaBackendRenderTarget::MakeGL(WINDOW_WIDTH, WINDOW_HEIGHT, 0, 0, nullptr));
|
||||
|
||||
// Main loop
|
||||
bool quit = false;
|
||||
while (!quit) {
|
||||
// Process events
|
||||
SDL_Event event;
|
||||
while (SDL_PollEvent(&event)) {
|
||||
if (event.type == SDL_QUIT) {
|
||||
quit = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Clear the canvas
|
||||
SkCanvas* canvas = surface->getCanvas();
|
||||
canvas->clear(SK_ColorWHITE);
|
||||
|
||||
// Draw cursor position
|
||||
int mouseX, mouseY;
|
||||
SDL_GetMouseState(&mouseX, &mouseY);
|
||||
SkPaint paint;
|
||||
paint.setColor(SK_ColorBLACK);
|
||||
canvas->drawCircle(mouseX, mouseY, 5, paint);
|
||||
|
||||
// Copy Skia surface to SDL texture
|
||||
SkPixmap pixmap;
|
||||
surface->peekPixels(&pixmap);
|
||||
SDL_Surface* sdlSurface = SDL_CreateRGBSurfaceFrom(pixmap.addr(), WINDOW_WIDTH, WINDOW_HEIGHT, 32, pixmap.rowBytes(), 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);
|
||||
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, sdlSurface);
|
||||
|
||||
// Clear the renderer
|
||||
SDL_RenderClear(renderer);
|
||||
|
||||
// Copy texture to renderer
|
||||
SDL_RenderCopy(renderer, texture, nullptr, nullptr);
|
||||
|
||||
// Render to window
|
||||
SDL_RenderPresent(renderer);
|
||||
|
||||
// Clean up
|
||||
SDL_FreeSurface(sdlSurface);
|
||||
SDL_DestroyTexture(texture);
|
||||
}
|
||||
|
||||
// Clean up
|
||||
SDL_DestroyRenderer(renderer);
|
||||
SDL_DestroyWindow(window);
|
||||
SDL_Quit();
|
||||
return 0;
|
||||
}
|
33
workspace.bazel
Normal file
33
workspace.bazel
Normal file
@ -0,0 +1,33 @@
|
||||
workspace(name = "nanote")
|
||||
|
||||
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
|
||||
|
||||
# Skia
|
||||
http_archive(
|
||||
name = "skia",
|
||||
urls = ["https://skia.googlesource.com/skia/+archive/refs/heads/main.tar.gz"],
|
||||
strip_prefix = "skia-main",
|
||||
)
|
||||
|
||||
# rules_foreign_cc
|
||||
http_archive(
|
||||
name = "rules_foreign_cc",
|
||||
urls = ["https://github.com/bazelbuild/rules_foreign_cc/archive/refs/tags/3.0.0.tar.gz"],
|
||||
strip_prefix = "rules_foreign_cc-3.0.0",
|
||||
)
|
||||
|
||||
load("@rules_foreign_cc//:workspace_definitions.bzl", "rules_foreign_cc_dependencies")
|
||||
|
||||
rules_foreign_cc_dependencies()
|
||||
|
||||
# SDL2
|
||||
http_archive(
|
||||
name = "sdl2",
|
||||
sha256 = "e2abf22f2ec34d36d1714ff9d624d10b86c67cf3cb345131b44804d7cbe37fd5",
|
||||
urls = ["https://github.com/bazelregistry/sdl2/archive/refs/tags/2.0.18.tar.gz"],
|
||||
strip_prefix = "sdl2-2.0.18",
|
||||
)
|
||||
|
||||
load("@sdl2//:workspace.bzl", "sdl2_repositories")
|
||||
|
||||
sdl2_repositories()
|
Loading…
Reference in New Issue
Block a user